| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 1 | // -*- C++ -*- | 
|  | 2 | //===----------------------------------------------------------------------===// | 
|  | 3 | // | 
| Howard Hinnant | f5256e1 | 2010-05-11 21:36:01 | [diff] [blame] | 4 | // The LLVM Compiler Infrastructure | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 5 | // | 
| Howard Hinnant | b64f8b0 | 2010-11-16 22:09:02 | [diff] [blame] | 6 | // This file is dual licensed under the MIT and the University of Illinois Open | 
|  | 7 | // Source Licenses. See LICENSE.TXT for details. | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 8 | // | 
|  | 9 | //===----------------------------------------------------------------------===// | 
|  | 10 |  | 
|  | 11 | #ifndef _LIBCPP___TREE | 
|  | 12 | #define _LIBCPP___TREE | 
|  | 13 |  | 
|  | 14 | #include <__config> | 
|  | 15 | #include <iterator> | 
|  | 16 | #include <memory> | 
|  | 17 | #include <stdexcept> | 
|  | 18 | #include <algorithm> | 
|  | 19 |  | 
| Howard Hinnant | 08e1747 | 2011-10-17 20:05:10 | [diff] [blame] | 20 | #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 21 | #pragma GCC system_header | 
| Howard Hinnant | 08e1747 | 2011-10-17 20:05:10 | [diff] [blame] | 22 | #endif | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 23 |  | 
|  | 24 | _LIBCPP_BEGIN_NAMESPACE_STD | 
|  | 25 |  | 
| Howard Hinnant | 2b1b2d4 | 2011-06-14 19:58:17 | [diff] [blame] | 26 | template <class _Tp, class _Compare, class _Allocator> class __tree; | 
|  | 27 | template <class _Tp, class _NodePtr, class _DiffType> | 
| Howard Hinnant | 0f678bd | 2013-08-12 18:38:34 | [diff] [blame] | 28 | class _LIBCPP_TYPE_VIS_ONLY __tree_iterator; | 
| Howard Hinnant | 2b1b2d4 | 2011-06-14 19:58:17 | [diff] [blame] | 29 | template <class _Tp, class _ConstNodePtr, class _DiffType> | 
| Howard Hinnant | 0f678bd | 2013-08-12 18:38:34 | [diff] [blame] | 30 | class _LIBCPP_TYPE_VIS_ONLY __tree_const_iterator; | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 31 |  | 
| Eric Fiselier | 5526348 | 2016-02-20 05:28:30 | [diff] [blame] | 32 | template <class _Pointer> class __tree_end_node; | 
|  | 33 | template <class _VoidPtr> class __tree_node_base; | 
|  | 34 | template <class _Tp, class _VoidPtr> class __tree_node; | 
|  | 35 |  | 
|  | 36 | #ifndef _LIBCPP_CXX03_LANG | 
|  | 37 | template <class _Key, class _Value> | 
|  | 38 | union __value_type; | 
|  | 39 | #else | 
|  | 40 | template <class _Key, class _Value> | 
|  | 41 | struct __value_type; | 
|  | 42 | #endif | 
|  | 43 |  | 
|  | 44 | template <class _Allocator> class __map_node_destructor; | 
|  | 45 | template <class _TreeIterator> class _LIBCPP_TYPE_VIS_ONLY __map_iterator; | 
|  | 46 | template <class _TreeIterator> class _LIBCPP_TYPE_VIS_ONLY __map_const_iterator; | 
|  | 47 |  | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 48 | /* | 
|  | 49 |  | 
|  | 50 | _NodePtr algorithms | 
|  | 51 |  | 
|  | 52 | The algorithms taking _NodePtr are red black tree algorithms. Those | 
|  | 53 | algorithms taking a parameter named __root should assume that __root | 
|  | 54 | points to a proper red black tree (unless otherwise specified). | 
|  | 55 |  | 
|  | 56 | Each algorithm herein assumes that __root->__parent_ points to a non-null | 
|  | 57 | structure which has a member __left_ which points back to __root. No other | 
|  | 58 | member is read or written to at __root->__parent_. | 
|  | 59 |  | 
|  | 60 | __root->__parent_ will be referred to below (in comments only) as end_node. | 
|  | 61 | end_node->__left_ is an externably accessible lvalue for __root, and can be | 
|  | 62 | changed by node insertion and removal (without explicit reference to end_node). | 
|  | 63 |  | 
|  | 64 | All nodes (with the exception of end_node), even the node referred to as | 
|  | 65 | __root, have a non-null __parent_ field. | 
|  | 66 |  | 
|  | 67 | */ | 
|  | 68 |  | 
|  | 69 | // Returns: true if __x is a left child of its parent, else false | 
|  | 70 | // Precondition: __x != nullptr. | 
|  | 71 | template <class _NodePtr> | 
| Howard Hinnant | 333f50d | 2010-09-21 20:16:37 | [diff] [blame] | 72 | inline _LIBCPP_INLINE_VISIBILITY | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 73 | bool | 
| Howard Hinnant | 8b53768 | 2011-06-04 17:10:24 | [diff] [blame] | 74 | __tree_is_left_child(_NodePtr __x) _NOEXCEPT | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 75 | { | 
|  | 76 | return __x == __x->__parent_->__left_; | 
|  | 77 | } | 
|  | 78 |  | 
|  | 79 | // Determintes if the subtree rooted at __x is a proper red black subtree. If | 
|  | 80 | // __x is a proper subtree, returns the black height (null counts as 1). If | 
|  | 81 | // __x is an improper subtree, returns 0. | 
|  | 82 | template <class _NodePtr> | 
|  | 83 | unsigned | 
|  | 84 | __tree_sub_invariant(_NodePtr __x) | 
|  | 85 | { | 
|  | 86 | if (__x == nullptr) | 
|  | 87 | return 1; | 
|  | 88 | // parent consistency checked by caller | 
|  | 89 | // check __x->__left_ consistency | 
|  | 90 | if (__x->__left_ != nullptr && __x->__left_->__parent_ != __x) | 
|  | 91 | return 0; | 
|  | 92 | // check __x->__right_ consistency | 
|  | 93 | if (__x->__right_ != nullptr && __x->__right_->__parent_ != __x) | 
|  | 94 | return 0; | 
|  | 95 | // check __x->__left_ != __x->__right_ unless both are nullptr | 
|  | 96 | if (__x->__left_ == __x->__right_ && __x->__left_ != nullptr) | 
|  | 97 | return 0; | 
|  | 98 | // If this is red, neither child can be red | 
|  | 99 | if (!__x->__is_black_) | 
|  | 100 | { | 
|  | 101 | if (__x->__left_ && !__x->__left_->__is_black_) | 
|  | 102 | return 0; | 
|  | 103 | if (__x->__right_ && !__x->__right_->__is_black_) | 
|  | 104 | return 0; | 
|  | 105 | } | 
|  | 106 | unsigned __h = __tree_sub_invariant(__x->__left_); | 
|  | 107 | if (__h == 0) | 
|  | 108 | return 0; // invalid left subtree | 
|  | 109 | if (__h != __tree_sub_invariant(__x->__right_)) | 
|  | 110 | return 0; // invalid or different height right subtree | 
|  | 111 | return __h + __x->__is_black_; // return black height of this node | 
|  | 112 | } | 
|  | 113 |  | 
|  | 114 | // Determintes if the red black tree rooted at __root is a proper red black tree. | 
|  | 115 | // __root == nullptr is a proper tree. Returns true is __root is a proper | 
|  | 116 | // red black tree, else returns false. | 
|  | 117 | template <class _NodePtr> | 
|  | 118 | bool | 
|  | 119 | __tree_invariant(_NodePtr __root) | 
|  | 120 | { | 
|  | 121 | if (__root == nullptr) | 
|  | 122 | return true; | 
|  | 123 | // check __x->__parent_ consistency | 
|  | 124 | if (__root->__parent_ == nullptr) | 
|  | 125 | return false; | 
|  | 126 | if (!__tree_is_left_child(__root)) | 
|  | 127 | return false; | 
|  | 128 | // root must be black | 
|  | 129 | if (!__root->__is_black_) | 
|  | 130 | return false; | 
|  | 131 | // do normal node checks | 
|  | 132 | return __tree_sub_invariant(__root) != 0; | 
|  | 133 | } | 
|  | 134 |  | 
|  | 135 | // Returns: pointer to the left-most node under __x. | 
|  | 136 | // Precondition: __x != nullptr. | 
|  | 137 | template <class _NodePtr> | 
| Howard Hinnant | 333f50d | 2010-09-21 20:16:37 | [diff] [blame] | 138 | inline _LIBCPP_INLINE_VISIBILITY | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 139 | _NodePtr | 
| Howard Hinnant | 8b53768 | 2011-06-04 17:10:24 | [diff] [blame] | 140 | __tree_min(_NodePtr __x) _NOEXCEPT | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 141 | { | 
|  | 142 | while (__x->__left_ != nullptr) | 
|  | 143 | __x = __x->__left_; | 
|  | 144 | return __x; | 
|  | 145 | } | 
|  | 146 |  | 
|  | 147 | // Returns: pointer to the right-most node under __x. | 
|  | 148 | // Precondition: __x != nullptr. | 
|  | 149 | template <class _NodePtr> | 
| Howard Hinnant | 333f50d | 2010-09-21 20:16:37 | [diff] [blame] | 150 | inline _LIBCPP_INLINE_VISIBILITY | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 151 | _NodePtr | 
| Howard Hinnant | 8b53768 | 2011-06-04 17:10:24 | [diff] [blame] | 152 | __tree_max(_NodePtr __x) _NOEXCEPT | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 153 | { | 
|  | 154 | while (__x->__right_ != nullptr) | 
|  | 155 | __x = __x->__right_; | 
|  | 156 | return __x; | 
|  | 157 | } | 
|  | 158 |  | 
|  | 159 | // Returns: pointer to the next in-order node after __x. | 
|  | 160 | // Precondition: __x != nullptr. | 
|  | 161 | template <class _NodePtr> | 
|  | 162 | _NodePtr | 
| Howard Hinnant | 8b53768 | 2011-06-04 17:10:24 | [diff] [blame] | 163 | __tree_next(_NodePtr __x) _NOEXCEPT | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 164 | { | 
|  | 165 | if (__x->__right_ != nullptr) | 
|  | 166 | return __tree_min(__x->__right_); | 
|  | 167 | while (!__tree_is_left_child(__x)) | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 168 | __x = __x->__parent_unsafe(); | 
|  | 169 | return __x->__parent_unsafe(); | 
|  | 170 | } | 
|  | 171 |  | 
|  | 172 | template <class _EndNodePtr, class _NodePtr> | 
|  | 173 | inline _LIBCPP_INLINE_VISIBILITY | 
|  | 174 | _EndNodePtr | 
|  | 175 | __tree_next_iter(_NodePtr __x) _NOEXCEPT | 
|  | 176 | { | 
|  | 177 | if (__x->__right_ != nullptr) | 
|  | 178 | return static_cast<_EndNodePtr>(__tree_min(__x->__right_)); | 
|  | 179 | while (!__tree_is_left_child(__x)) | 
|  | 180 | __x = __x->__parent_unsafe(); | 
|  | 181 | return static_cast<_EndNodePtr>(__x->__parent_); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 182 | } | 
|  | 183 |  | 
|  | 184 | // Returns: pointer to the previous in-order node before __x. | 
|  | 185 | // Precondition: __x != nullptr. | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 186 | // Note: __x may be the end node. | 
|  | 187 | template <class _NodePtr, class _EndNodePtr> | 
|  | 188 | inline _LIBCPP_INLINE_VISIBILITY | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 189 | _NodePtr | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 190 | __tree_prev_iter(_EndNodePtr __x) _NOEXCEPT | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 191 | { | 
|  | 192 | if (__x->__left_ != nullptr) | 
|  | 193 | return __tree_max(__x->__left_); | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 194 | _NodePtr __xx = static_cast<_NodePtr>(__x); | 
|  | 195 | while (__tree_is_left_child(__xx)) | 
|  | 196 | __xx = __xx->__parent_unsafe(); | 
|  | 197 | return __xx->__parent_unsafe(); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 198 | } | 
|  | 199 |  | 
|  | 200 | // Returns: pointer to a node which has no children | 
|  | 201 | // Precondition: __x != nullptr. | 
|  | 202 | template <class _NodePtr> | 
|  | 203 | _NodePtr | 
| Howard Hinnant | 8b53768 | 2011-06-04 17:10:24 | [diff] [blame] | 204 | __tree_leaf(_NodePtr __x) _NOEXCEPT | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 205 | { | 
|  | 206 | while (true) | 
|  | 207 | { | 
|  | 208 | if (__x->__left_ != nullptr) | 
|  | 209 | { | 
|  | 210 | __x = __x->__left_; | 
|  | 211 | continue; | 
|  | 212 | } | 
|  | 213 | if (__x->__right_ != nullptr) | 
|  | 214 | { | 
|  | 215 | __x = __x->__right_; | 
|  | 216 | continue; | 
|  | 217 | } | 
|  | 218 | break; | 
|  | 219 | } | 
|  | 220 | return __x; | 
|  | 221 | } | 
|  | 222 |  | 
|  | 223 | // Effects: Makes __x->__right_ the subtree root with __x as its left child | 
|  | 224 | // while preserving in-order order. | 
|  | 225 | // Precondition: __x->__right_ != nullptr | 
|  | 226 | template <class _NodePtr> | 
|  | 227 | void | 
| Howard Hinnant | 8b53768 | 2011-06-04 17:10:24 | [diff] [blame] | 228 | __tree_left_rotate(_NodePtr __x) _NOEXCEPT | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 229 | { | 
|  | 230 | _NodePtr __y = __x->__right_; | 
|  | 231 | __x->__right_ = __y->__left_; | 
|  | 232 | if (__x->__right_ != nullptr) | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 233 | __x->__right_->__set_parent(__x); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 234 | __y->__parent_ = __x->__parent_; | 
|  | 235 | if (__tree_is_left_child(__x)) | 
|  | 236 | __x->__parent_->__left_ = __y; | 
|  | 237 | else | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 238 | __x->__parent_unsafe()->__right_ = __y; | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 239 | __y->__left_ = __x; | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 240 | __x->__set_parent(__y); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 241 | } | 
|  | 242 |  | 
|  | 243 | // Effects: Makes __x->__left_ the subtree root with __x as its right child | 
|  | 244 | // while preserving in-order order. | 
|  | 245 | // Precondition: __x->__left_ != nullptr | 
|  | 246 | template <class _NodePtr> | 
|  | 247 | void | 
| Howard Hinnant | 8b53768 | 2011-06-04 17:10:24 | [diff] [blame] | 248 | __tree_right_rotate(_NodePtr __x) _NOEXCEPT | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 249 | { | 
|  | 250 | _NodePtr __y = __x->__left_; | 
|  | 251 | __x->__left_ = __y->__right_; | 
|  | 252 | if (__x->__left_ != nullptr) | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 253 | __x->__left_->__set_parent(__x); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 254 | __y->__parent_ = __x->__parent_; | 
|  | 255 | if (__tree_is_left_child(__x)) | 
|  | 256 | __x->__parent_->__left_ = __y; | 
|  | 257 | else | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 258 | __x->__parent_unsafe()->__right_ = __y; | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 259 | __y->__right_ = __x; | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 260 | __x->__set_parent(__y); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 261 | } | 
|  | 262 |  | 
|  | 263 | // Effects: Rebalances __root after attaching __x to a leaf. | 
|  | 264 | // Precondition: __root != nulptr && __x != nullptr. | 
|  | 265 | // __x has no children. | 
|  | 266 | // __x == __root or == a direct or indirect child of __root. | 
|  | 267 | // If __x were to be unlinked from __root (setting __root to | 
|  | 268 | // nullptr if __root == __x), __tree_invariant(__root) == true. | 
|  | 269 | // Postcondition: __tree_invariant(end_node->__left_) == true. end_node->__left_ | 
|  | 270 | // may be different than the value passed in as __root. | 
|  | 271 | template <class _NodePtr> | 
|  | 272 | void | 
| Howard Hinnant | 8b53768 | 2011-06-04 17:10:24 | [diff] [blame] | 273 | __tree_balance_after_insert(_NodePtr __root, _NodePtr __x) _NOEXCEPT | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 274 | { | 
|  | 275 | __x->__is_black_ = __x == __root; | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 276 | while (__x != __root && !__x->__parent_unsafe()->__is_black_) | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 277 | { | 
|  | 278 | // __x->__parent_ != __root because __x->__parent_->__is_black == false | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 279 | if (__tree_is_left_child(__x->__parent_unsafe())) | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 280 | { | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 281 | _NodePtr __y = __x->__parent_unsafe()->__parent_unsafe()->__right_; | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 282 | if (__y != nullptr && !__y->__is_black_) | 
|  | 283 | { | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 284 | __x = __x->__parent_unsafe(); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 285 | __x->__is_black_ = true; | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 286 | __x = __x->__parent_unsafe(); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 287 | __x->__is_black_ = __x == __root; | 
|  | 288 | __y->__is_black_ = true; | 
|  | 289 | } | 
|  | 290 | else | 
|  | 291 | { | 
|  | 292 | if (!__tree_is_left_child(__x)) | 
|  | 293 | { | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 294 | __x = __x->__parent_unsafe(); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 295 | __tree_left_rotate(__x); | 
|  | 296 | } | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 297 | __x = __x->__parent_unsafe(); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 298 | __x->__is_black_ = true; | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 299 | __x = __x->__parent_unsafe(); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 300 | __x->__is_black_ = false; | 
|  | 301 | __tree_right_rotate(__x); | 
|  | 302 | break; | 
|  | 303 | } | 
|  | 304 | } | 
|  | 305 | else | 
|  | 306 | { | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 307 | _NodePtr __y = __x->__parent_unsafe()->__parent_->__left_; | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 308 | if (__y != nullptr && !__y->__is_black_) | 
|  | 309 | { | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 310 | __x = __x->__parent_unsafe(); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 311 | __x->__is_black_ = true; | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 312 | __x = __x->__parent_unsafe(); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 313 | __x->__is_black_ = __x == __root; | 
|  | 314 | __y->__is_black_ = true; | 
|  | 315 | } | 
|  | 316 | else | 
|  | 317 | { | 
|  | 318 | if (__tree_is_left_child(__x)) | 
|  | 319 | { | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 320 | __x = __x->__parent_unsafe(); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 321 | __tree_right_rotate(__x); | 
|  | 322 | } | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 323 | __x = __x->__parent_unsafe(); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 324 | __x->__is_black_ = true; | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 325 | __x = __x->__parent_unsafe(); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 326 | __x->__is_black_ = false; | 
|  | 327 | __tree_left_rotate(__x); | 
|  | 328 | break; | 
|  | 329 | } | 
|  | 330 | } | 
|  | 331 | } | 
|  | 332 | } | 
|  | 333 |  | 
|  | 334 | // Precondition: __root != nullptr && __z != nullptr. | 
|  | 335 | // __tree_invariant(__root) == true. | 
|  | 336 | // __z == __root or == a direct or indirect child of __root. | 
|  | 337 | // Effects: unlinks __z from the tree rooted at __root, rebalancing as needed. | 
|  | 338 | // Postcondition: __tree_invariant(end_node->__left_) == true && end_node->__left_ | 
|  | 339 | // nor any of its children refer to __z. end_node->__left_ | 
|  | 340 | // may be different than the value passed in as __root. | 
|  | 341 | template <class _NodePtr> | 
|  | 342 | void | 
| Howard Hinnant | 8b53768 | 2011-06-04 17:10:24 | [diff] [blame] | 343 | __tree_remove(_NodePtr __root, _NodePtr __z) _NOEXCEPT | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 344 | { | 
|  | 345 | // __z will be removed from the tree. Client still needs to destruct/deallocate it | 
|  | 346 | // __y is either __z, or if __z has two children, __tree_next(__z). | 
|  | 347 | // __y will have at most one child. | 
|  | 348 | // __y will be the initial hole in the tree (make the hole at a leaf) | 
|  | 349 | _NodePtr __y = (__z->__left_ == nullptr || __z->__right_ == nullptr) ? | 
|  | 350 | __z : __tree_next(__z); | 
|  | 351 | // __x is __y's possibly null single child | 
|  | 352 | _NodePtr __x = __y->__left_ != nullptr ? __y->__left_ : __y->__right_; | 
|  | 353 | // __w is __x's possibly null uncle (will become __x's sibling) | 
|  | 354 | _NodePtr __w = nullptr; | 
|  | 355 | // link __x to __y's parent, and find __w | 
|  | 356 | if (__x != nullptr) | 
|  | 357 | __x->__parent_ = __y->__parent_; | 
|  | 358 | if (__tree_is_left_child(__y)) | 
|  | 359 | { | 
|  | 360 | __y->__parent_->__left_ = __x; | 
|  | 361 | if (__y != __root) | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 362 | __w = __y->__parent_unsafe()->__right_; | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 363 | else | 
|  | 364 | __root = __x; // __w == nullptr | 
|  | 365 | } | 
|  | 366 | else | 
|  | 367 | { | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 368 | __y->__parent_unsafe()->__right_ = __x; | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 369 | // __y can't be root if it is a right child | 
|  | 370 | __w = __y->__parent_->__left_; | 
|  | 371 | } | 
|  | 372 | bool __removed_black = __y->__is_black_; | 
|  | 373 | // If we didn't remove __z, do so now by splicing in __y for __z, | 
|  | 374 | // but copy __z's color. This does not impact __x or __w. | 
|  | 375 | if (__y != __z) | 
|  | 376 | { | 
| Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 | [diff] [blame] | 377 | // __z->__left_ != nulptr but __z->__right_ might == __x == nullptr | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 378 | __y->__parent_ = __z->__parent_; | 
|  | 379 | if (__tree_is_left_child(__z)) | 
|  | 380 | __y->__parent_->__left_ = __y; | 
|  | 381 | else | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 382 | __y->__parent_unsafe()->__right_ = __y; | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 383 | __y->__left_ = __z->__left_; | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 384 | __y->__left_->__set_parent(__y); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 385 | __y->__right_ = __z->__right_; | 
|  | 386 | if (__y->__right_ != nullptr) | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 387 | __y->__right_->__set_parent(__y); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 388 | __y->__is_black_ = __z->__is_black_; | 
|  | 389 | if (__root == __z) | 
|  | 390 | __root = __y; | 
|  | 391 | } | 
|  | 392 | // There is no need to rebalance if we removed a red, or if we removed | 
|  | 393 | // the last node. | 
|  | 394 | if (__removed_black && __root != nullptr) | 
|  | 395 | { | 
|  | 396 | // Rebalance: | 
|  | 397 | // __x has an implicit black color (transferred from the removed __y) | 
|  | 398 | // associated with it, no matter what its color is. | 
|  | 399 | // If __x is __root (in which case it can't be null), it is supposed | 
|  | 400 | // to be black anyway, and if it is doubly black, then the double | 
|  | 401 | // can just be ignored. | 
|  | 402 | // If __x is red (in which case it can't be null), then it can absorb | 
|  | 403 | // the implicit black just by setting its color to black. | 
|  | 404 | // Since __y was black and only had one child (which __x points to), __x | 
|  | 405 | // is either red with no children, else null, otherwise __y would have | 
|  | 406 | // different black heights under left and right pointers. | 
|  | 407 | // if (__x == __root || __x != nullptr && !__x->__is_black_) | 
|  | 408 | if (__x != nullptr) | 
|  | 409 | __x->__is_black_ = true; | 
|  | 410 | else | 
|  | 411 | { | 
|  | 412 | // Else __x isn't root, and is "doubly black", even though it may | 
|  | 413 | // be null. __w can not be null here, else the parent would | 
|  | 414 | // see a black height >= 2 on the __x side and a black height | 
|  | 415 | // of 1 on the __w side (__w must be a non-null black or a red | 
|  | 416 | // with a non-null black child). | 
|  | 417 | while (true) | 
|  | 418 | { | 
|  | 419 | if (!__tree_is_left_child(__w)) // if x is left child | 
|  | 420 | { | 
|  | 421 | if (!__w->__is_black_) | 
|  | 422 | { | 
|  | 423 | __w->__is_black_ = true; | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 424 | __w->__parent_unsafe()->__is_black_ = false; | 
|  | 425 | __tree_left_rotate(__w->__parent_unsafe()); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 426 | // __x is still valid | 
|  | 427 | // reset __root only if necessary | 
|  | 428 | if (__root == __w->__left_) | 
|  | 429 | __root = __w; | 
|  | 430 | // reset sibling, and it still can't be null | 
|  | 431 | __w = __w->__left_->__right_; | 
|  | 432 | } | 
|  | 433 | // __w->__is_black_ is now true, __w may have null children | 
|  | 434 | if ((__w->__left_ == nullptr || __w->__left_->__is_black_) && | 
|  | 435 | (__w->__right_ == nullptr || __w->__right_->__is_black_)) | 
|  | 436 | { | 
|  | 437 | __w->__is_black_ = false; | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 438 | __x = __w->__parent_unsafe(); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 439 | // __x can no longer be null | 
|  | 440 | if (__x == __root || !__x->__is_black_) | 
|  | 441 | { | 
|  | 442 | __x->__is_black_ = true; | 
|  | 443 | break; | 
|  | 444 | } | 
|  | 445 | // reset sibling, and it still can't be null | 
|  | 446 | __w = __tree_is_left_child(__x) ? | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 447 | __x->__parent_unsafe()->__right_ : | 
| Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 | [diff] [blame] | 448 | __x->__parent_->__left_; | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 449 | // continue; | 
|  | 450 | } | 
|  | 451 | else // __w has a red child | 
|  | 452 | { | 
|  | 453 | if (__w->__right_ == nullptr || __w->__right_->__is_black_) | 
|  | 454 | { | 
|  | 455 | // __w left child is non-null and red | 
|  | 456 | __w->__left_->__is_black_ = true; | 
|  | 457 | __w->__is_black_ = false; | 
|  | 458 | __tree_right_rotate(__w); | 
|  | 459 | // __w is known not to be root, so root hasn't changed | 
|  | 460 | // reset sibling, and it still can't be null | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 461 | __w = __w->__parent_unsafe(); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 462 | } | 
|  | 463 | // __w has a right red child, left child may be null | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 464 | __w->__is_black_ = __w->__parent_unsafe()->__is_black_; | 
|  | 465 | __w->__parent_unsafe()->__is_black_ = true; | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 466 | __w->__right_->__is_black_ = true; | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 467 | __tree_left_rotate(__w->__parent_unsafe()); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 468 | break; | 
|  | 469 | } | 
|  | 470 | } | 
|  | 471 | else | 
|  | 472 | { | 
|  | 473 | if (!__w->__is_black_) | 
|  | 474 | { | 
|  | 475 | __w->__is_black_ = true; | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 476 | __w->__parent_unsafe()->__is_black_ = false; | 
|  | 477 | __tree_right_rotate(__w->__parent_unsafe()); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 478 | // __x is still valid | 
|  | 479 | // reset __root only if necessary | 
|  | 480 | if (__root == __w->__right_) | 
|  | 481 | __root = __w; | 
|  | 482 | // reset sibling, and it still can't be null | 
|  | 483 | __w = __w->__right_->__left_; | 
|  | 484 | } | 
|  | 485 | // __w->__is_black_ is now true, __w may have null children | 
|  | 486 | if ((__w->__left_ == nullptr || __w->__left_->__is_black_) && | 
|  | 487 | (__w->__right_ == nullptr || __w->__right_->__is_black_)) | 
|  | 488 | { | 
|  | 489 | __w->__is_black_ = false; | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 490 | __x = __w->__parent_unsafe(); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 491 | // __x can no longer be null | 
|  | 492 | if (!__x->__is_black_ || __x == __root) | 
|  | 493 | { | 
|  | 494 | __x->__is_black_ = true; | 
|  | 495 | break; | 
|  | 496 | } | 
|  | 497 | // reset sibling, and it still can't be null | 
|  | 498 | __w = __tree_is_left_child(__x) ? | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 499 | __x->__parent_unsafe()->__right_ : | 
| Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 | [diff] [blame] | 500 | __x->__parent_->__left_; | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 501 | // continue; | 
|  | 502 | } | 
|  | 503 | else // __w has a red child | 
|  | 504 | { | 
|  | 505 | if (__w->__left_ == nullptr || __w->__left_->__is_black_) | 
|  | 506 | { | 
|  | 507 | // __w right child is non-null and red | 
|  | 508 | __w->__right_->__is_black_ = true; | 
|  | 509 | __w->__is_black_ = false; | 
|  | 510 | __tree_left_rotate(__w); | 
|  | 511 | // __w is known not to be root, so root hasn't changed | 
|  | 512 | // reset sibling, and it still can't be null | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 513 | __w = __w->__parent_unsafe(); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 514 | } | 
|  | 515 | // __w has a left red child, right child may be null | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 516 | __w->__is_black_ = __w->__parent_unsafe()->__is_black_; | 
|  | 517 | __w->__parent_unsafe()->__is_black_ = true; | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 518 | __w->__left_->__is_black_ = true; | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 519 | __tree_right_rotate(__w->__parent_unsafe()); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 520 | break; | 
|  | 521 | } | 
|  | 522 | } | 
|  | 523 | } | 
|  | 524 | } | 
|  | 525 | } | 
|  | 526 | } | 
|  | 527 |  | 
| Eric Fiselier | 5526348 | 2016-02-20 05:28:30 | [diff] [blame] | 528 | // node traits | 
|  | 529 |  | 
| Eric Fiselier | db21506 | 2016-03-31 02:15:15 | [diff] [blame] | 530 |  | 
|  | 531 | #ifndef _LIBCPP_CXX03_LANG | 
|  | 532 | template <class _Tp> | 
|  | 533 | struct __is_tree_value_type_imp : false_type {}; | 
|  | 534 |  | 
|  | 535 | template <class _Key, class _Value> | 
|  | 536 | struct __is_tree_value_type_imp<__value_type<_Key, _Value>> : true_type {}; | 
|  | 537 |  | 
|  | 538 | template <class ..._Args> | 
|  | 539 | struct __is_tree_value_type : false_type {}; | 
|  | 540 |  | 
|  | 541 | template <class _One> | 
|  | 542 | struct __is_tree_value_type<_One> : __is_tree_value_type_imp<typename __uncvref<_One>::type> {}; | 
|  | 543 | #endif | 
|  | 544 |  | 
| Eric Fiselier | 5526348 | 2016-02-20 05:28:30 | [diff] [blame] | 545 | template <class _Tp> | 
|  | 546 | struct __tree_key_value_types { | 
|  | 547 | typedef _Tp key_type; | 
|  | 548 | typedef _Tp __node_value_type; | 
|  | 549 | typedef _Tp __container_value_type; | 
|  | 550 | static const bool __is_map = false; | 
| Eric Fiselier | db21506 | 2016-03-31 02:15:15 | [diff] [blame] | 551 |  | 
|  | 552 | _LIBCPP_INLINE_VISIBILITY | 
|  | 553 | static key_type const& __get_key(_Tp const& __v) { | 
|  | 554 | return __v; | 
|  | 555 | } | 
|  | 556 | _LIBCPP_INLINE_VISIBILITY | 
|  | 557 | static __container_value_type const& __get_value(__node_value_type const& __v) { | 
|  | 558 | return __v; | 
|  | 559 | } | 
|  | 560 | _LIBCPP_INLINE_VISIBILITY | 
|  | 561 | static __container_value_type* __get_ptr(__node_value_type& __n) { | 
|  | 562 | return _VSTD::addressof(__n); | 
|  | 563 | } | 
|  | 564 |  | 
|  | 565 | #ifndef _LIBCPP_CXX03_LANG | 
|  | 566 | _LIBCPP_INLINE_VISIBILITY | 
|  | 567 | static __container_value_type&& __move(__node_value_type& __v) { | 
|  | 568 | return _VSTD::move(__v); | 
|  | 569 | } | 
|  | 570 | #endif | 
| Eric Fiselier | 5526348 | 2016-02-20 05:28:30 | [diff] [blame] | 571 | }; | 
|  | 572 |  | 
|  | 573 | template <class _Key, class _Tp> | 
|  | 574 | struct __tree_key_value_types<__value_type<_Key, _Tp> > { | 
|  | 575 | typedef _Key key_type; | 
|  | 576 | typedef _Tp mapped_type; | 
|  | 577 | typedef __value_type<_Key, _Tp> __node_value_type; | 
|  | 578 | typedef pair<const _Key, _Tp> __container_value_type; | 
|  | 579 | typedef pair<_Key, _Tp> __nc_value_type; | 
|  | 580 | typedef __container_value_type __map_value_type; | 
|  | 581 | static const bool __is_map = true; | 
| Eric Fiselier | db21506 | 2016-03-31 02:15:15 | [diff] [blame] | 582 |  | 
|  | 583 | _LIBCPP_INLINE_VISIBILITY | 
|  | 584 | static key_type const& | 
|  | 585 | __get_key(__node_value_type const& __t) { | 
|  | 586 | return __t.__cc.first; | 
|  | 587 | } | 
|  | 588 |  | 
|  | 589 | template <class _Up> | 
|  | 590 | _LIBCPP_INLINE_VISIBILITY | 
|  | 591 | static typename enable_if<__is_same_uncvref<_Up, __container_value_type>::value, | 
|  | 592 | key_type const&>::type | 
|  | 593 | __get_key(_Up& __t) { | 
|  | 594 | return __t.first; | 
|  | 595 | } | 
|  | 596 |  | 
|  | 597 | _LIBCPP_INLINE_VISIBILITY | 
|  | 598 | static __container_value_type const& | 
|  | 599 | __get_value(__node_value_type const& __t) { | 
|  | 600 | return __t.__cc; | 
|  | 601 | } | 
|  | 602 |  | 
|  | 603 | template <class _Up> | 
|  | 604 | _LIBCPP_INLINE_VISIBILITY | 
|  | 605 | static typename enable_if<__is_same_uncvref<_Up, __container_value_type>::value, | 
|  | 606 | __container_value_type const&>::type | 
|  | 607 | __get_value(_Up& __t) { | 
|  | 608 | return __t; | 
|  | 609 | } | 
|  | 610 |  | 
|  | 611 | _LIBCPP_INLINE_VISIBILITY | 
|  | 612 | static __container_value_type* __get_ptr(__node_value_type& __n) { | 
|  | 613 | return _VSTD::addressof(__n.__cc); | 
|  | 614 | } | 
|  | 615 |  | 
|  | 616 | #ifndef _LIBCPP_CXX03_LANG | 
|  | 617 | _LIBCPP_INLINE_VISIBILITY | 
|  | 618 | static __nc_value_type&& __move(__node_value_type& __v) { | 
|  | 619 | return _VSTD::move(__v.__nc); | 
|  | 620 | } | 
|  | 621 | #endif | 
| Eric Fiselier | 5526348 | 2016-02-20 05:28:30 | [diff] [blame] | 622 | }; | 
|  | 623 |  | 
|  | 624 | template <class _VoidPtr> | 
|  | 625 | struct __tree_node_base_types { | 
|  | 626 | typedef _VoidPtr __void_pointer; | 
|  | 627 |  | 
|  | 628 | typedef __tree_node_base<__void_pointer> __node_base_type; | 
|  | 629 | typedef typename __rebind_pointer<_VoidPtr, __node_base_type>::type | 
|  | 630 | __node_base_pointer; | 
|  | 631 |  | 
|  | 632 | typedef __tree_end_node<__node_base_pointer> __end_node_type; | 
|  | 633 | typedef typename __rebind_pointer<_VoidPtr, __end_node_type>::type | 
|  | 634 | __end_node_pointer; | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 635 | #if defined(_LIBCPP_ABI_TREE_REMOVE_NODE_POINTER_UB) | 
|  | 636 | typedef __end_node_pointer __parent_pointer; | 
|  | 637 | #else | 
|  | 638 | typedef typename conditional< | 
|  | 639 | is_pointer<__end_node_pointer>::value, | 
|  | 640 | __end_node_pointer, | 
|  | 641 | __node_base_pointer>::type __parent_pointer; | 
|  | 642 | #endif | 
|  | 643 |  | 
| Eric Fiselier | 5526348 | 2016-02-20 05:28:30 | [diff] [blame] | 644 | private: | 
|  | 645 | static_assert((is_same<typename pointer_traits<_VoidPtr>::element_type, void>::value), | 
|  | 646 | "_VoidPtr does not point to unqualified void type"); | 
|  | 647 | }; | 
|  | 648 |  | 
|  | 649 | template <class _Tp, class _AllocPtr, class _KVTypes = __tree_key_value_types<_Tp>, | 
|  | 650 | bool = _KVTypes::__is_map> | 
|  | 651 | struct __tree_map_pointer_types {}; | 
|  | 652 |  | 
|  | 653 | template <class _Tp, class _AllocPtr, class _KVTypes> | 
|  | 654 | struct __tree_map_pointer_types<_Tp, _AllocPtr, _KVTypes, true> { | 
|  | 655 | typedef typename _KVTypes::__map_value_type _Mv; | 
|  | 656 | typedef typename __rebind_pointer<_AllocPtr, _Mv>::type | 
|  | 657 | __map_value_type_pointer; | 
|  | 658 | typedef typename __rebind_pointer<_AllocPtr, const _Mv>::type | 
|  | 659 | __const_map_value_type_pointer; | 
|  | 660 | }; | 
|  | 661 |  | 
|  | 662 | template <class _NodePtr, class _NodeT = typename pointer_traits<_NodePtr>::element_type> | 
|  | 663 | struct __tree_node_types; | 
|  | 664 |  | 
|  | 665 | template <class _NodePtr, class _Tp, class _VoidPtr> | 
|  | 666 | struct __tree_node_types<_NodePtr, __tree_node<_Tp, _VoidPtr> > | 
|  | 667 | : public __tree_node_base_types<_VoidPtr>, | 
|  | 668 | __tree_key_value_types<_Tp>, | 
|  | 669 | __tree_map_pointer_types<_Tp, _VoidPtr> | 
|  | 670 | { | 
|  | 671 | typedef __tree_node_base_types<_VoidPtr> __base; | 
|  | 672 | typedef __tree_key_value_types<_Tp> __key_base; | 
|  | 673 | typedef __tree_map_pointer_types<_Tp, _VoidPtr> __map_pointer_base; | 
|  | 674 | public: | 
|  | 675 |  | 
|  | 676 | typedef typename pointer_traits<_NodePtr>::element_type __node_type; | 
|  | 677 | typedef _NodePtr __node_pointer; | 
|  | 678 |  | 
|  | 679 | typedef _Tp __node_value_type; | 
|  | 680 | typedef typename __rebind_pointer<_VoidPtr, __node_value_type>::type | 
|  | 681 | __node_value_type_pointer; | 
|  | 682 | typedef typename __rebind_pointer<_VoidPtr, const __node_value_type>::type | 
|  | 683 | __const_node_value_type_pointer; | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 684 | #if defined(_LIBCPP_ABI_TREE_REMOVE_NODE_POINTER_UB) | 
|  | 685 | typedef typename __base::__end_node_pointer __iter_pointer; | 
|  | 686 | #else | 
|  | 687 | typedef typename conditional< | 
|  | 688 | is_pointer<__node_pointer>::value, | 
|  | 689 | typename __base::__end_node_pointer, | 
|  | 690 | __node_pointer>::type __iter_pointer; | 
|  | 691 | #endif | 
| Eric Fiselier | 5526348 | 2016-02-20 05:28:30 | [diff] [blame] | 692 | private: | 
|  | 693 | static_assert(!is_const<__node_type>::value, | 
|  | 694 | "_NodePtr should never be a pointer to const"); | 
|  | 695 | static_assert((is_same<typename __rebind_pointer<_VoidPtr, __node_type>::type, | 
|  | 696 | _NodePtr>::value), "_VoidPtr does not rebind to _NodePtr."); | 
|  | 697 | }; | 
|  | 698 |  | 
|  | 699 | template <class _ValueTp, class _VoidPtr> | 
|  | 700 | struct __make_tree_node_types { | 
|  | 701 | typedef typename __rebind_pointer<_VoidPtr, __tree_node<_ValueTp, _VoidPtr> >::type | 
|  | 702 | _NodePtr; | 
|  | 703 | typedef __tree_node_types<_NodePtr> type; | 
|  | 704 | }; | 
|  | 705 |  | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 706 | // node | 
|  | 707 |  | 
|  | 708 | template <class _Pointer> | 
|  | 709 | class __tree_end_node | 
|  | 710 | { | 
|  | 711 | public: | 
|  | 712 | typedef _Pointer pointer; | 
|  | 713 | pointer __left_; | 
|  | 714 |  | 
| Howard Hinnant | 333f50d | 2010-09-21 20:16:37 | [diff] [blame] | 715 | _LIBCPP_INLINE_VISIBILITY | 
| Howard Hinnant | 8b53768 | 2011-06-04 17:10:24 | [diff] [blame] | 716 | __tree_end_node() _NOEXCEPT : __left_() {} | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 717 | }; | 
|  | 718 |  | 
|  | 719 | template <class _VoidPtr> | 
|  | 720 | class __tree_node_base | 
| Eric Fiselier | 5526348 | 2016-02-20 05:28:30 | [diff] [blame] | 721 | : public __tree_node_base_types<_VoidPtr>::__end_node_type | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 722 | { | 
| Eric Fiselier | 5526348 | 2016-02-20 05:28:30 | [diff] [blame] | 723 | typedef __tree_node_base_types<_VoidPtr> _NodeBaseTypes; | 
|  | 724 |  | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 725 | public: | 
| Eric Fiselier | 5526348 | 2016-02-20 05:28:30 | [diff] [blame] | 726 | typedef typename _NodeBaseTypes::__node_base_pointer pointer; | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 727 | typedef typename _NodeBaseTypes::__parent_pointer __parent_pointer; | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 728 |  | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 729 | pointer __right_; | 
|  | 730 | __parent_pointer __parent_; | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 731 | bool __is_black_; | 
|  | 732 |  | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 733 | _LIBCPP_INLINE_VISIBILITY | 
|  | 734 | pointer __parent_unsafe() const { return static_cast<pointer>(__parent_);} | 
|  | 735 |  | 
|  | 736 | _LIBCPP_INLINE_VISIBILITY | 
|  | 737 | void __set_parent(pointer __p) { | 
|  | 738 | __parent_ = static_cast<__parent_pointer>(__p); | 
|  | 739 | } | 
|  | 740 |  | 
| Eric Fiselier | db21506 | 2016-03-31 02:15:15 | [diff] [blame] | 741 | private: | 
|  | 742 | ~__tree_node_base() _LIBCPP_EQUAL_DELETE; | 
|  | 743 | __tree_node_base(__tree_node_base const&) _LIBCPP_EQUAL_DELETE; | 
|  | 744 | __tree_node_base& operator=(__tree_node_base const&) _LIBCPP_EQUAL_DELETE; | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 745 | }; | 
|  | 746 |  | 
|  | 747 | template <class _Tp, class _VoidPtr> | 
|  | 748 | class __tree_node | 
|  | 749 | : public __tree_node_base<_VoidPtr> | 
|  | 750 | { | 
|  | 751 | public: | 
| Eric Fiselier | 5526348 | 2016-02-20 05:28:30 | [diff] [blame] | 752 | typedef _Tp __node_value_type; | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 753 |  | 
| Eric Fiselier | 5526348 | 2016-02-20 05:28:30 | [diff] [blame] | 754 | __node_value_type __value_; | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 755 |  | 
| Eric Fiselier | db21506 | 2016-03-31 02:15:15 | [diff] [blame] | 756 | private: | 
|  | 757 | ~__tree_node() _LIBCPP_EQUAL_DELETE; | 
|  | 758 | __tree_node(__tree_node const&) _LIBCPP_EQUAL_DELETE; | 
|  | 759 | __tree_node& operator=(__tree_node const&) _LIBCPP_EQUAL_DELETE; | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 760 | }; | 
|  | 761 |  | 
| Eric Fiselier | db21506 | 2016-03-31 02:15:15 | [diff] [blame] | 762 |  | 
|  | 763 | template <class _Allocator> | 
|  | 764 | class __tree_node_destructor | 
|  | 765 | { | 
|  | 766 | typedef _Allocator allocator_type; | 
|  | 767 | typedef allocator_traits<allocator_type> __alloc_traits; | 
|  | 768 |  | 
|  | 769 | public: | 
|  | 770 | typedef typename __alloc_traits::pointer pointer; | 
|  | 771 | private: | 
|  | 772 | typedef __tree_node_types<pointer> _NodeTypes; | 
|  | 773 | allocator_type& __na_; | 
|  | 774 |  | 
|  | 775 | __tree_node_destructor& operator=(const __tree_node_destructor&); | 
|  | 776 |  | 
|  | 777 | public: | 
|  | 778 | bool __value_constructed; | 
|  | 779 |  | 
|  | 780 | _LIBCPP_INLINE_VISIBILITY | 
|  | 781 | explicit __tree_node_destructor(allocator_type& __na, bool __val = false) _NOEXCEPT | 
|  | 782 | : __na_(__na), | 
|  | 783 | __value_constructed(__val) | 
|  | 784 | {} | 
|  | 785 |  | 
|  | 786 | _LIBCPP_INLINE_VISIBILITY | 
|  | 787 | void operator()(pointer __p) _NOEXCEPT | 
|  | 788 | { | 
|  | 789 | if (__value_constructed) | 
|  | 790 | __alloc_traits::destroy(__na_, _NodeTypes::__get_ptr(__p->__value_)); | 
|  | 791 | if (__p) | 
|  | 792 | __alloc_traits::deallocate(__na_, __p, 1); | 
|  | 793 | } | 
|  | 794 |  | 
|  | 795 | template <class> friend class __map_node_destructor; | 
|  | 796 | }; | 
|  | 797 |  | 
|  | 798 |  | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 799 | template <class _Tp, class _NodePtr, class _DiffType> | 
| Howard Hinnant | 0f678bd | 2013-08-12 18:38:34 | [diff] [blame] | 800 | class _LIBCPP_TYPE_VIS_ONLY __tree_iterator | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 801 | { | 
| Eric Fiselier | 5526348 | 2016-02-20 05:28:30 | [diff] [blame] | 802 | typedef __tree_node_types<_NodePtr> _NodeTypes; | 
|  | 803 | typedef _NodePtr __node_pointer; | 
|  | 804 | typedef typename _NodeTypes::__node_base_pointer __node_base_pointer; | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 805 | typedef typename _NodeTypes::__end_node_pointer __end_node_pointer; | 
|  | 806 | typedef typename _NodeTypes::__iter_pointer __iter_pointer; | 
| Eric Fiselier | 5526348 | 2016-02-20 05:28:30 | [diff] [blame] | 807 | typedef pointer_traits<__node_pointer> __pointer_traits; | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 808 |  | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 809 | __iter_pointer __ptr_; | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 810 |  | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 811 | public: | 
| Eric Fiselier | 5526348 | 2016-02-20 05:28:30 | [diff] [blame] | 812 | typedef bidirectional_iterator_tag iterator_category; | 
|  | 813 | typedef _Tp value_type; | 
|  | 814 | typedef _DiffType difference_type; | 
|  | 815 | typedef value_type& reference; | 
|  | 816 | typedef typename _NodeTypes::__node_value_type_pointer pointer; | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 817 |  | 
| Marshall Clow | 051c848 | 2013-08-08 21:52:50 | [diff] [blame] | 818 | _LIBCPP_INLINE_VISIBILITY __tree_iterator() _NOEXCEPT | 
|  | 819 | #if _LIBCPP_STD_VER > 11 | 
|  | 820 | : __ptr_(nullptr) | 
|  | 821 | #endif | 
|  | 822 | {} | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 823 |  | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 824 | _LIBCPP_INLINE_VISIBILITY reference operator*() const | 
|  | 825 | {return __get_np()->__value_;} | 
| Howard Hinnant | 70342b9 | 2013-06-19 21:29:40 | [diff] [blame] | 826 | _LIBCPP_INLINE_VISIBILITY pointer operator->() const | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 827 | {return pointer_traits<pointer>::pointer_to(__get_np()->__value_);} | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 828 |  | 
| Howard Hinnant | 333f50d | 2010-09-21 20:16:37 | [diff] [blame] | 829 | _LIBCPP_INLINE_VISIBILITY | 
| Eric Fiselier | 9c8e663 | 2015-03-03 20:10:01 | [diff] [blame] | 830 | __tree_iterator& operator++() { | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 831 | __ptr_ = static_cast<__iter_pointer>( | 
|  | 832 | __tree_next_iter<__end_node_pointer>(static_cast<__node_base_pointer>(__ptr_))); | 
| Eric Fiselier | 9c8e663 | 2015-03-03 20:10:01 | [diff] [blame] | 833 | return *this; | 
|  | 834 | } | 
| Howard Hinnant | 333f50d | 2010-09-21 20:16:37 | [diff] [blame] | 835 | _LIBCPP_INLINE_VISIBILITY | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 836 | __tree_iterator operator++(int) | 
|  | 837 | {__tree_iterator __t(*this); ++(*this); return __t;} | 
|  | 838 |  | 
| Howard Hinnant | 333f50d | 2010-09-21 20:16:37 | [diff] [blame] | 839 | _LIBCPP_INLINE_VISIBILITY | 
| Eric Fiselier | 9c8e663 | 2015-03-03 20:10:01 | [diff] [blame] | 840 | __tree_iterator& operator--() { | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 841 | __ptr_ = static_cast<__iter_pointer>(__tree_prev_iter<__node_base_pointer>( | 
|  | 842 | static_cast<__end_node_pointer>(__ptr_))); | 
| Eric Fiselier | 9c8e663 | 2015-03-03 20:10:01 | [diff] [blame] | 843 | return *this; | 
|  | 844 | } | 
| Howard Hinnant | 333f50d | 2010-09-21 20:16:37 | [diff] [blame] | 845 | _LIBCPP_INLINE_VISIBILITY | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 846 | __tree_iterator operator--(int) | 
|  | 847 | {__tree_iterator __t(*this); --(*this); return __t;} | 
|  | 848 |  | 
| Howard Hinnant | 333f50d | 2010-09-21 20:16:37 | [diff] [blame] | 849 | friend _LIBCPP_INLINE_VISIBILITY | 
|  | 850 | bool operator==(const __tree_iterator& __x, const __tree_iterator& __y) | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 851 | {return __x.__ptr_ == __y.__ptr_;} | 
| Howard Hinnant | 333f50d | 2010-09-21 20:16:37 | [diff] [blame] | 852 | friend _LIBCPP_INLINE_VISIBILITY | 
|  | 853 | bool operator!=(const __tree_iterator& __x, const __tree_iterator& __y) | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 854 | {return !(__x == __y);} | 
|  | 855 |  | 
|  | 856 | private: | 
| Howard Hinnant | 333f50d | 2010-09-21 20:16:37 | [diff] [blame] | 857 | _LIBCPP_INLINE_VISIBILITY | 
| Howard Hinnant | 7686add | 2011-06-04 14:31:57 | [diff] [blame] | 858 | explicit __tree_iterator(__node_pointer __p) _NOEXCEPT : __ptr_(__p) {} | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 859 | _LIBCPP_INLINE_VISIBILITY | 
|  | 860 | explicit __tree_iterator(__end_node_pointer __p) _NOEXCEPT : __ptr_(__p) {} | 
|  | 861 | _LIBCPP_INLINE_VISIBILITY | 
|  | 862 | __node_pointer __get_np() const { return static_cast<__node_pointer>(__ptr_); } | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 863 | template <class, class, class> friend class __tree; | 
| Howard Hinnant | 0f678bd | 2013-08-12 18:38:34 | [diff] [blame] | 864 | template <class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY __tree_const_iterator; | 
|  | 865 | template <class> friend class _LIBCPP_TYPE_VIS_ONLY __map_iterator; | 
|  | 866 | template <class, class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY map; | 
|  | 867 | template <class, class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY multimap; | 
|  | 868 | template <class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY set; | 
|  | 869 | template <class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY multiset; | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 870 | }; | 
|  | 871 |  | 
| Eric Fiselier | 5526348 | 2016-02-20 05:28:30 | [diff] [blame] | 872 | template <class _Tp, class _NodePtr, class _DiffType> | 
| Howard Hinnant | 0f678bd | 2013-08-12 18:38:34 | [diff] [blame] | 873 | class _LIBCPP_TYPE_VIS_ONLY __tree_const_iterator | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 874 | { | 
| Eric Fiselier | 5526348 | 2016-02-20 05:28:30 | [diff] [blame] | 875 | typedef __tree_node_types<_NodePtr> _NodeTypes; | 
|  | 876 | typedef typename _NodeTypes::__node_pointer __node_pointer; | 
|  | 877 | typedef typename _NodeTypes::__node_base_pointer __node_base_pointer; | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 878 | typedef typename _NodeTypes::__end_node_pointer __end_node_pointer; | 
|  | 879 | typedef typename _NodeTypes::__iter_pointer __iter_pointer; | 
| Eric Fiselier | 5526348 | 2016-02-20 05:28:30 | [diff] [blame] | 880 | typedef pointer_traits<__node_pointer> __pointer_traits; | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 881 |  | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 882 | __iter_pointer __ptr_; | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 883 |  | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 884 | public: | 
| Eric Fiselier | 5526348 | 2016-02-20 05:28:30 | [diff] [blame] | 885 | typedef bidirectional_iterator_tag iterator_category; | 
|  | 886 | typedef _Tp value_type; | 
|  | 887 | typedef _DiffType difference_type; | 
|  | 888 | typedef const value_type& reference; | 
|  | 889 | typedef typename _NodeTypes::__const_node_value_type_pointer pointer; | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 890 |  | 
| Marshall Clow | 051c848 | 2013-08-08 21:52:50 | [diff] [blame] | 891 | _LIBCPP_INLINE_VISIBILITY __tree_const_iterator() _NOEXCEPT | 
|  | 892 | #if _LIBCPP_STD_VER > 11 | 
|  | 893 | : __ptr_(nullptr) | 
|  | 894 | #endif | 
|  | 895 | {} | 
|  | 896 |  | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 897 | private: | 
| Eric Fiselier | 5526348 | 2016-02-20 05:28:30 | [diff] [blame] | 898 | typedef __tree_iterator<value_type, __node_pointer, difference_type> | 
|  | 899 | __non_const_iterator; | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 900 | public: | 
| Howard Hinnant | 333f50d | 2010-09-21 20:16:37 | [diff] [blame] | 901 | _LIBCPP_INLINE_VISIBILITY | 
| Howard Hinnant | 7686add | 2011-06-04 14:31:57 | [diff] [blame] | 902 | __tree_const_iterator(__non_const_iterator __p) _NOEXCEPT | 
|  | 903 | : __ptr_(__p.__ptr_) {} | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 904 |  | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 905 | _LIBCPP_INLINE_VISIBILITY reference operator*() const | 
|  | 906 | {return __get_np()->__value_;} | 
| Howard Hinnant | 70342b9 | 2013-06-19 21:29:40 | [diff] [blame] | 907 | _LIBCPP_INLINE_VISIBILITY pointer operator->() const | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 908 | {return pointer_traits<pointer>::pointer_to(__get_np()->__value_);} | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 909 |  | 
| Howard Hinnant | 333f50d | 2010-09-21 20:16:37 | [diff] [blame] | 910 | _LIBCPP_INLINE_VISIBILITY | 
| Eric Fiselier | 9c8e663 | 2015-03-03 20:10:01 | [diff] [blame] | 911 | __tree_const_iterator& operator++() { | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 912 | __ptr_ = static_cast<__iter_pointer>( | 
|  | 913 | __tree_next_iter<__end_node_pointer>(static_cast<__node_base_pointer>(__ptr_))); | 
| Eric Fiselier | 9c8e663 | 2015-03-03 20:10:01 | [diff] [blame] | 914 | return *this; | 
|  | 915 | } | 
|  | 916 |  | 
| Howard Hinnant | 333f50d | 2010-09-21 20:16:37 | [diff] [blame] | 917 | _LIBCPP_INLINE_VISIBILITY | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 918 | __tree_const_iterator operator++(int) | 
|  | 919 | {__tree_const_iterator __t(*this); ++(*this); return __t;} | 
|  | 920 |  | 
| Howard Hinnant | 333f50d | 2010-09-21 20:16:37 | [diff] [blame] | 921 | _LIBCPP_INLINE_VISIBILITY | 
| Eric Fiselier | 9c8e663 | 2015-03-03 20:10:01 | [diff] [blame] | 922 | __tree_const_iterator& operator--() { | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 923 | __ptr_ = static_cast<__iter_pointer>(__tree_prev_iter<__node_base_pointer>( | 
|  | 924 | static_cast<__end_node_pointer>(__ptr_))); | 
| Eric Fiselier | 9c8e663 | 2015-03-03 20:10:01 | [diff] [blame] | 925 | return *this; | 
|  | 926 | } | 
|  | 927 |  | 
| Howard Hinnant | 333f50d | 2010-09-21 20:16:37 | [diff] [blame] | 928 | _LIBCPP_INLINE_VISIBILITY | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 929 | __tree_const_iterator operator--(int) | 
|  | 930 | {__tree_const_iterator __t(*this); --(*this); return __t;} | 
|  | 931 |  | 
| Howard Hinnant | 333f50d | 2010-09-21 20:16:37 | [diff] [blame] | 932 | friend _LIBCPP_INLINE_VISIBILITY | 
|  | 933 | bool operator==(const __tree_const_iterator& __x, const __tree_const_iterator& __y) | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 934 | {return __x.__ptr_ == __y.__ptr_;} | 
| Howard Hinnant | 333f50d | 2010-09-21 20:16:37 | [diff] [blame] | 935 | friend _LIBCPP_INLINE_VISIBILITY | 
|  | 936 | bool operator!=(const __tree_const_iterator& __x, const __tree_const_iterator& __y) | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 937 | {return !(__x == __y);} | 
|  | 938 |  | 
|  | 939 | private: | 
| Howard Hinnant | 333f50d | 2010-09-21 20:16:37 | [diff] [blame] | 940 | _LIBCPP_INLINE_VISIBILITY | 
| Howard Hinnant | 7686add | 2011-06-04 14:31:57 | [diff] [blame] | 941 | explicit __tree_const_iterator(__node_pointer __p) _NOEXCEPT | 
|  | 942 | : __ptr_(__p) {} | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 943 | _LIBCPP_INLINE_VISIBILITY | 
|  | 944 | explicit __tree_const_iterator(__end_node_pointer __p) _NOEXCEPT | 
|  | 945 | : __ptr_(__p) {} | 
|  | 946 | _LIBCPP_INLINE_VISIBILITY | 
|  | 947 | __node_pointer __get_np() const { return static_cast<__node_pointer>(__ptr_); } | 
|  | 948 |  | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 949 | template <class, class, class> friend class __tree; | 
| Howard Hinnant | 0f678bd | 2013-08-12 18:38:34 | [diff] [blame] | 950 | template <class, class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY map; | 
|  | 951 | template <class, class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY multimap; | 
|  | 952 | template <class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY set; | 
|  | 953 | template <class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY multiset; | 
|  | 954 | template <class> friend class _LIBCPP_TYPE_VIS_ONLY __map_const_iterator; | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 955 |  | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 956 | }; | 
|  | 957 |  | 
|  | 958 | template <class _Tp, class _Compare, class _Allocator> | 
|  | 959 | class __tree | 
|  | 960 | { | 
|  | 961 | public: | 
|  | 962 | typedef _Tp value_type; | 
|  | 963 | typedef _Compare value_compare; | 
|  | 964 | typedef _Allocator allocator_type; | 
| Eric Fiselier | 5526348 | 2016-02-20 05:28:30 | [diff] [blame] | 965 |  | 
|  | 966 | private: | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 967 | typedef allocator_traits<allocator_type> __alloc_traits; | 
| Eric Fiselier | 5526348 | 2016-02-20 05:28:30 | [diff] [blame] | 968 | typedef typename __make_tree_node_types<value_type, | 
|  | 969 | typename __alloc_traits::void_pointer>::type | 
|  | 970 | _NodeTypes; | 
| Eric Fiselier | db21506 | 2016-03-31 02:15:15 | [diff] [blame] | 971 | typedef typename _NodeTypes::key_type key_type; | 
| Eric Fiselier | 5526348 | 2016-02-20 05:28:30 | [diff] [blame] | 972 | public: | 
|  | 973 | typedef typename _NodeTypes::__node_value_type __node_value_type; | 
|  | 974 | typedef typename _NodeTypes::__container_value_type __container_value_type; | 
|  | 975 |  | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 976 | typedef typename __alloc_traits::pointer pointer; | 
|  | 977 | typedef typename __alloc_traits::const_pointer const_pointer; | 
|  | 978 | typedef typename __alloc_traits::size_type size_type; | 
|  | 979 | typedef typename __alloc_traits::difference_type difference_type; | 
|  | 980 |  | 
| Eric Fiselier | 5526348 | 2016-02-20 05:28:30 | [diff] [blame] | 981 | public: | 
|  | 982 | typedef typename _NodeTypes::__void_pointer __void_pointer; | 
| Howard Hinnant | 70342b9 | 2013-06-19 21:29:40 | [diff] [blame] | 983 |  | 
| Eric Fiselier | 5526348 | 2016-02-20 05:28:30 | [diff] [blame] | 984 | typedef typename _NodeTypes::__node_type __node; | 
|  | 985 | typedef typename _NodeTypes::__node_pointer __node_pointer; | 
| Eric Fiselier | 5526348 | 2016-02-20 05:28:30 | [diff] [blame] | 986 |  | 
|  | 987 | typedef typename _NodeTypes::__node_base_type __node_base; | 
|  | 988 | typedef typename _NodeTypes::__node_base_pointer __node_base_pointer; | 
| Eric Fiselier | 5526348 | 2016-02-20 05:28:30 | [diff] [blame] | 989 |  | 
|  | 990 | typedef typename _NodeTypes::__end_node_type __end_node_t; | 
|  | 991 | typedef typename _NodeTypes::__end_node_pointer __end_node_ptr; | 
| Eric Fiselier | 5526348 | 2016-02-20 05:28:30 | [diff] [blame] | 992 |  | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 993 | typedef typename _NodeTypes::__parent_pointer __parent_pointer; | 
|  | 994 | typedef typename _NodeTypes::__iter_pointer __iter_pointer; | 
|  | 995 |  | 
| Marshall Clow | 66302c6 | 2015-04-07 05:21:38 | [diff] [blame] | 996 | typedef typename __rebind_alloc_helper<__alloc_traits, __node>::type __node_allocator; | 
| Eric Fiselier | 5526348 | 2016-02-20 05:28:30 | [diff] [blame] | 997 | typedef allocator_traits<__node_allocator> __node_traits; | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 998 |  | 
| Eric Fiselier | 5526348 | 2016-02-20 05:28:30 | [diff] [blame] | 999 | private: | 
|  | 1000 | // check for sane allocator pointer rebinding semantics. Rebinding the | 
|  | 1001 | // allocator for a new pointer type should be exactly the same as rebinding | 
|  | 1002 | // the pointer using 'pointer_traits'. | 
|  | 1003 | static_assert((is_same<__node_pointer, typename __node_traits::pointer>::value), | 
|  | 1004 | "Allocator does not rebind pointers in a sane manner."); | 
|  | 1005 | typedef typename __rebind_alloc_helper<__node_traits, __node_base>::type | 
|  | 1006 | __node_base_allocator; | 
|  | 1007 | typedef allocator_traits<__node_base_allocator> __node_base_traits; | 
|  | 1008 | static_assert((is_same<__node_base_pointer, typename __node_base_traits::pointer>::value), | 
|  | 1009 | "Allocator does not rebind pointers in a sane manner."); | 
| Eric Fiselier | 5526348 | 2016-02-20 05:28:30 | [diff] [blame] | 1010 |  | 
|  | 1011 | private: | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 1012 | __iter_pointer __begin_node_; | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 1013 | __compressed_pair<__end_node_t, __node_allocator> __pair1_; | 
|  | 1014 | __compressed_pair<size_type, value_compare> __pair3_; | 
|  | 1015 |  | 
|  | 1016 | public: | 
| Howard Hinnant | 333f50d | 2010-09-21 20:16:37 | [diff] [blame] | 1017 | _LIBCPP_INLINE_VISIBILITY | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 1018 | __iter_pointer __end_node() _NOEXCEPT | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 1019 | { | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 1020 | return static_cast<__iter_pointer>( | 
| Eric Fiselier | 227b47c | 2016-02-20 07:12:17 | [diff] [blame] | 1021 | pointer_traits<__end_node_ptr>::pointer_to(__pair1_.first()) | 
|  | 1022 | ); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 1023 | } | 
| Howard Hinnant | 333f50d | 2010-09-21 20:16:37 | [diff] [blame] | 1024 | _LIBCPP_INLINE_VISIBILITY | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 1025 | __iter_pointer __end_node() const _NOEXCEPT | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 1026 | { | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 1027 | return static_cast<__iter_pointer>( | 
| Eric Fiselier | 227b47c | 2016-02-20 07:12:17 | [diff] [blame] | 1028 | pointer_traits<__end_node_ptr>::pointer_to( | 
|  | 1029 | const_cast<__end_node_t&>(__pair1_.first()) | 
|  | 1030 | ) | 
|  | 1031 | ); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 1032 | } | 
| Howard Hinnant | 333f50d | 2010-09-21 20:16:37 | [diff] [blame] | 1033 | _LIBCPP_INLINE_VISIBILITY | 
| Howard Hinnant | 7686add | 2011-06-04 14:31:57 | [diff] [blame] | 1034 | __node_allocator& __node_alloc() _NOEXCEPT {return __pair1_.second();} | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 1035 | private: | 
| Howard Hinnant | 333f50d | 2010-09-21 20:16:37 | [diff] [blame] | 1036 | _LIBCPP_INLINE_VISIBILITY | 
| Howard Hinnant | 7686add | 2011-06-04 14:31:57 | [diff] [blame] | 1037 | const __node_allocator& __node_alloc() const _NOEXCEPT | 
|  | 1038 | {return __pair1_.second();} | 
| Howard Hinnant | 333f50d | 2010-09-21 20:16:37 | [diff] [blame] | 1039 | _LIBCPP_INLINE_VISIBILITY | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 1040 | __iter_pointer& __begin_node() _NOEXCEPT {return __begin_node_;} | 
| Howard Hinnant | 333f50d | 2010-09-21 20:16:37 | [diff] [blame] | 1041 | _LIBCPP_INLINE_VISIBILITY | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 1042 | const __iter_pointer& __begin_node() const _NOEXCEPT {return __begin_node_;} | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 1043 | public: | 
| Howard Hinnant | 333f50d | 2010-09-21 20:16:37 | [diff] [blame] | 1044 | _LIBCPP_INLINE_VISIBILITY | 
| Howard Hinnant | 7686add | 2011-06-04 14:31:57 | [diff] [blame] | 1045 | allocator_type __alloc() const _NOEXCEPT | 
|  | 1046 | {return allocator_type(__node_alloc());} | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 1047 | private: | 
| Howard Hinnant | 333f50d | 2010-09-21 20:16:37 | [diff] [blame] | 1048 | _LIBCPP_INLINE_VISIBILITY | 
| Howard Hinnant | 7686add | 2011-06-04 14:31:57 | [diff] [blame] | 1049 | size_type& size() _NOEXCEPT {return __pair3_.first();} | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 1050 | public: | 
| Howard Hinnant | 333f50d | 2010-09-21 20:16:37 | [diff] [blame] | 1051 | _LIBCPP_INLINE_VISIBILITY | 
| Howard Hinnant | 7686add | 2011-06-04 14:31:57 | [diff] [blame] | 1052 | const size_type& size() const _NOEXCEPT {return __pair3_.first();} | 
| Howard Hinnant | 333f50d | 2010-09-21 20:16:37 | [diff] [blame] | 1053 | _LIBCPP_INLINE_VISIBILITY | 
| Howard Hinnant | 7686add | 2011-06-04 14:31:57 | [diff] [blame] | 1054 | value_compare& value_comp() _NOEXCEPT {return __pair3_.second();} | 
| Howard Hinnant | 333f50d | 2010-09-21 20:16:37 | [diff] [blame] | 1055 | _LIBCPP_INLINE_VISIBILITY | 
| Howard Hinnant | 7686add | 2011-06-04 14:31:57 | [diff] [blame] | 1056 | const value_compare& value_comp() const _NOEXCEPT | 
|  | 1057 | {return __pair3_.second();} | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 1058 | public: | 
| Eric Fiselier | 227b47c | 2016-02-20 07:12:17 | [diff] [blame] | 1059 |  | 
| Howard Hinnant | 333f50d | 2010-09-21 20:16:37 | [diff] [blame] | 1060 | _LIBCPP_INLINE_VISIBILITY | 
| Eric Fiselier | 227b47c | 2016-02-20 07:12:17 | [diff] [blame] | 1061 | __node_pointer __root() const _NOEXCEPT | 
|  | 1062 | {return static_cast<__node_pointer>(__end_node()->__left_);} | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 1063 |  | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 1064 | __node_base_pointer* __root_ptr() const _NOEXCEPT { | 
|  | 1065 | return _VSTD::addressof(__end_node()->__left_); | 
|  | 1066 | } | 
|  | 1067 |  | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 1068 | typedef __tree_iterator<value_type, __node_pointer, difference_type> iterator; | 
| Howard Hinnant | 70342b9 | 2013-06-19 21:29:40 | [diff] [blame] | 1069 | typedef __tree_const_iterator<value_type, __node_pointer, difference_type> const_iterator; | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 1070 |  | 
| Howard Hinnant | 7686add | 2011-06-04 14:31:57 | [diff] [blame] | 1071 | explicit __tree(const value_compare& __comp) | 
|  | 1072 | _NOEXCEPT_( | 
|  | 1073 | is_nothrow_default_constructible<__node_allocator>::value && | 
|  | 1074 | is_nothrow_copy_constructible<value_compare>::value); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 1075 | explicit __tree(const allocator_type& __a); | 
|  | 1076 | __tree(const value_compare& __comp, const allocator_type& __a); | 
|  | 1077 | __tree(const __tree& __t); | 
|  | 1078 | __tree& operator=(const __tree& __t); | 
|  | 1079 | template <class _InputIterator> | 
|  | 1080 | void __assign_unique(_InputIterator __first, _InputIterator __last); | 
|  | 1081 | template <class _InputIterator> | 
|  | 1082 | void __assign_multi(_InputIterator __first, _InputIterator __last); | 
| Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 | [diff] [blame] | 1083 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES | 
| Howard Hinnant | 7686add | 2011-06-04 14:31:57 | [diff] [blame] | 1084 | __tree(__tree&& __t) | 
|  | 1085 | _NOEXCEPT_( | 
|  | 1086 | is_nothrow_move_constructible<__node_allocator>::value && | 
|  | 1087 | is_nothrow_move_constructible<value_compare>::value); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 1088 | __tree(__tree&& __t, const allocator_type& __a); | 
| Howard Hinnant | 7686add | 2011-06-04 14:31:57 | [diff] [blame] | 1089 | __tree& operator=(__tree&& __t) | 
|  | 1090 | _NOEXCEPT_( | 
|  | 1091 | __node_traits::propagate_on_container_move_assignment::value && | 
|  | 1092 | is_nothrow_move_assignable<value_compare>::value && | 
|  | 1093 | is_nothrow_move_assignable<__node_allocator>::value); | 
| Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 | [diff] [blame] | 1094 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 1095 |  | 
|  | 1096 | ~__tree(); | 
|  | 1097 |  | 
| Howard Hinnant | 333f50d | 2010-09-21 20:16:37 | [diff] [blame] | 1098 | _LIBCPP_INLINE_VISIBILITY | 
| Howard Hinnant | 7686add | 2011-06-04 14:31:57 | [diff] [blame] | 1099 | iterator begin() _NOEXCEPT {return iterator(__begin_node());} | 
| Howard Hinnant | 333f50d | 2010-09-21 20:16:37 | [diff] [blame] | 1100 | _LIBCPP_INLINE_VISIBILITY | 
| Howard Hinnant | 7686add | 2011-06-04 14:31:57 | [diff] [blame] | 1101 | const_iterator begin() const _NOEXCEPT {return const_iterator(__begin_node());} | 
| Howard Hinnant | 333f50d | 2010-09-21 20:16:37 | [diff] [blame] | 1102 | _LIBCPP_INLINE_VISIBILITY | 
| Howard Hinnant | 7686add | 2011-06-04 14:31:57 | [diff] [blame] | 1103 | iterator end() _NOEXCEPT {return iterator(__end_node());} | 
| Howard Hinnant | 333f50d | 2010-09-21 20:16:37 | [diff] [blame] | 1104 | _LIBCPP_INLINE_VISIBILITY | 
| Howard Hinnant | 7686add | 2011-06-04 14:31:57 | [diff] [blame] | 1105 | const_iterator end() const _NOEXCEPT {return const_iterator(__end_node());} | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 1106 |  | 
| Howard Hinnant | 333f50d | 2010-09-21 20:16:37 | [diff] [blame] | 1107 | _LIBCPP_INLINE_VISIBILITY | 
| Howard Hinnant | 7686add | 2011-06-04 14:31:57 | [diff] [blame] | 1108 | size_type max_size() const _NOEXCEPT | 
|  | 1109 | {return __node_traits::max_size(__node_alloc());} | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 1110 |  | 
| Howard Hinnant | 7686add | 2011-06-04 14:31:57 | [diff] [blame] | 1111 | void clear() _NOEXCEPT; | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 1112 |  | 
| Howard Hinnant | 7686add | 2011-06-04 14:31:57 | [diff] [blame] | 1113 | void swap(__tree& __t) | 
| Dimitry Andric | 1421cf0 | 2016-08-27 19:32:03 | [diff] [blame] | 1114 | #if _LIBCPP_STD_VER <= 11 | 
| Howard Hinnant | 7686add | 2011-06-04 14:31:57 | [diff] [blame] | 1115 | _NOEXCEPT_( | 
| Marshall Clow | 7d914d1 | 2015-07-13 20:04:56 | [diff] [blame] | 1116 | __is_nothrow_swappable<value_compare>::value | 
| Marshall Clow | 7d914d1 | 2015-07-13 20:04:56 | [diff] [blame] | 1117 | && (!__node_traits::propagate_on_container_swap::value || | 
|  | 1118 | __is_nothrow_swappable<__node_allocator>::value) | 
| Marshall Clow | 7d914d1 | 2015-07-13 20:04:56 | [diff] [blame] | 1119 | ); | 
| Dimitry Andric | 1421cf0 | 2016-08-27 19:32:03 | [diff] [blame] | 1120 | #else | 
|  | 1121 | _NOEXCEPT_(__is_nothrow_swappable<value_compare>::value); | 
|  | 1122 | #endif | 
| Eric Fiselier | db21506 | 2016-03-31 02:15:15 | [diff] [blame] | 1123 |  | 
|  | 1124 | #ifndef _LIBCPP_CXX03_LANG | 
|  | 1125 | template <class _Key, class ..._Args> | 
|  | 1126 | pair<iterator, bool> | 
|  | 1127 | __emplace_unique_key_args(_Key const&, _Args&&... __args); | 
|  | 1128 | template <class _Key, class ..._Args> | 
|  | 1129 | iterator | 
|  | 1130 | __emplace_hint_unique_key_args(const_iterator, _Key const&, _Args&&...); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 1131 |  | 
|  | 1132 | template <class... _Args> | 
| Eric Fiselier | 83c9dc1 | 2016-04-15 23:27:27 | [diff] [blame] | 1133 | pair<iterator, bool> __emplace_unique_impl(_Args&&... __args); | 
| Eric Fiselier | db21506 | 2016-03-31 02:15:15 | [diff] [blame] | 1134 |  | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 1135 | template <class... _Args> | 
| Eric Fiselier | 83c9dc1 | 2016-04-15 23:27:27 | [diff] [blame] | 1136 | iterator __emplace_hint_unique_impl(const_iterator __p, _Args&&... __args); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 1137 |  | 
| Eric Fiselier | db21506 | 2016-03-31 02:15:15 | [diff] [blame] | 1138 | template <class... _Args> | 
|  | 1139 | iterator __emplace_multi(_Args&&... __args); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 1140 |  | 
| Eric Fiselier | db21506 | 2016-03-31 02:15:15 | [diff] [blame] | 1141 | template <class... _Args> | 
|  | 1142 | iterator __emplace_hint_multi(const_iterator __p, _Args&&... __args); | 
| Eric Fiselier | 83c9dc1 | 2016-04-15 23:27:27 | [diff] [blame] | 1143 |  | 
|  | 1144 | template <class _Pp> | 
|  | 1145 | _LIBCPP_INLINE_VISIBILITY | 
|  | 1146 | pair<iterator, bool> __emplace_unique(_Pp&& __x) { | 
|  | 1147 | return __emplace_unique_extract_key(_VSTD::forward<_Pp>(__x), | 
|  | 1148 | __can_extract_key<_Pp, key_type>()); | 
|  | 1149 | } | 
|  | 1150 |  | 
| Eric Fiselier | dc414cd | 2016-04-16 00:23:12 | [diff] [blame] | 1151 | template <class _First, class _Second> | 
|  | 1152 | _LIBCPP_INLINE_VISIBILITY | 
|  | 1153 | typename enable_if< | 
|  | 1154 | __can_extract_map_key<_First, key_type, __container_value_type>::value, | 
|  | 1155 | pair<iterator, bool> | 
|  | 1156 | >::type __emplace_unique(_First&& __f, _Second&& __s) { | 
|  | 1157 | return __emplace_unique_key_args(__f, _VSTD::forward<_First>(__f), | 
|  | 1158 | _VSTD::forward<_Second>(__s)); | 
|  | 1159 | } | 
|  | 1160 |  | 
| Eric Fiselier | 83c9dc1 | 2016-04-15 23:27:27 | [diff] [blame] | 1161 | template <class... _Args> | 
|  | 1162 | _LIBCPP_INLINE_VISIBILITY | 
|  | 1163 | pair<iterator, bool> __emplace_unique(_Args&&... __args) { | 
|  | 1164 | return __emplace_unique_impl(_VSTD::forward<_Args>(__args)...); | 
|  | 1165 | } | 
|  | 1166 |  | 
|  | 1167 | template <class _Pp> | 
|  | 1168 | _LIBCPP_INLINE_VISIBILITY | 
|  | 1169 | pair<iterator, bool> | 
|  | 1170 | __emplace_unique_extract_key(_Pp&& __x, __extract_key_fail_tag) { | 
|  | 1171 | return __emplace_unique_impl(_VSTD::forward<_Pp>(__x)); | 
|  | 1172 | } | 
|  | 1173 |  | 
|  | 1174 | template <class _Pp> | 
|  | 1175 | _LIBCPP_INLINE_VISIBILITY | 
|  | 1176 | pair<iterator, bool> | 
|  | 1177 | __emplace_unique_extract_key(_Pp&& __x, __extract_key_self_tag) { | 
|  | 1178 | return __emplace_unique_key_args(__x, _VSTD::forward<_Pp>(__x)); | 
|  | 1179 | } | 
|  | 1180 |  | 
|  | 1181 | template <class _Pp> | 
|  | 1182 | _LIBCPP_INLINE_VISIBILITY | 
|  | 1183 | pair<iterator, bool> | 
|  | 1184 | __emplace_unique_extract_key(_Pp&& __x, __extract_key_first_tag) { | 
|  | 1185 | return __emplace_unique_key_args(__x.first, _VSTD::forward<_Pp>(__x)); | 
|  | 1186 | } | 
|  | 1187 |  | 
|  | 1188 | template <class _Pp> | 
|  | 1189 | _LIBCPP_INLINE_VISIBILITY | 
|  | 1190 | iterator __emplace_hint_unique(const_iterator __p, _Pp&& __x) { | 
|  | 1191 | return __emplace_hint_unique_extract_key(__p, _VSTD::forward<_Pp>(__x), | 
|  | 1192 | __can_extract_key<_Pp, key_type>()); | 
|  | 1193 | } | 
|  | 1194 |  | 
| Eric Fiselier | dc414cd | 2016-04-16 00:23:12 | [diff] [blame] | 1195 | template <class _First, class _Second> | 
|  | 1196 | _LIBCPP_INLINE_VISIBILITY | 
|  | 1197 | typename enable_if< | 
|  | 1198 | __can_extract_map_key<_First, key_type, __container_value_type>::value, | 
|  | 1199 | iterator | 
|  | 1200 | >::type __emplace_hint_unique(const_iterator __p, _First&& __f, _Second&& __s) { | 
|  | 1201 | return __emplace_hint_unique_key_args(__p, __f, | 
|  | 1202 | _VSTD::forward<_First>(__f), | 
|  | 1203 | _VSTD::forward<_Second>(__s)); | 
|  | 1204 | } | 
|  | 1205 |  | 
| Eric Fiselier | 83c9dc1 | 2016-04-15 23:27:27 | [diff] [blame] | 1206 | template <class... _Args> | 
|  | 1207 | _LIBCPP_INLINE_VISIBILITY | 
|  | 1208 | iterator __emplace_hint_unique(const_iterator __p, _Args&&... __args) { | 
|  | 1209 | return __emplace_hint_unique_impl(__p, _VSTD::forward<_Args>(__args)...); | 
|  | 1210 | } | 
|  | 1211 |  | 
|  | 1212 | template <class _Pp> | 
|  | 1213 | _LIBCPP_INLINE_VISIBILITY | 
|  | 1214 | iterator | 
|  | 1215 | __emplace_hint_unique_extract_key(const_iterator __p, _Pp&& __x, __extract_key_fail_tag) { | 
|  | 1216 | return __emplace_hint_unique_impl(__p, _VSTD::forward<_Pp>(__x)); | 
|  | 1217 | } | 
|  | 1218 |  | 
|  | 1219 | template <class _Pp> | 
|  | 1220 | _LIBCPP_INLINE_VISIBILITY | 
|  | 1221 | iterator | 
|  | 1222 | __emplace_hint_unique_extract_key(const_iterator __p, _Pp&& __x, __extract_key_self_tag) { | 
|  | 1223 | return __emplace_hint_unique_key_args(__p, __x, _VSTD::forward<_Pp>(__x)); | 
|  | 1224 | } | 
|  | 1225 |  | 
|  | 1226 | template <class _Pp> | 
|  | 1227 | _LIBCPP_INLINE_VISIBILITY | 
|  | 1228 | iterator | 
|  | 1229 | __emplace_hint_unique_extract_key(const_iterator __p, _Pp&& __x, __extract_key_first_tag) { | 
|  | 1230 | return __emplace_hint_unique_key_args(__p, __x.first, _VSTD::forward<_Pp>(__x)); | 
|  | 1231 | } | 
|  | 1232 |  | 
| Eric Fiselier | db21506 | 2016-03-31 02:15:15 | [diff] [blame] | 1233 | #else | 
|  | 1234 | template <class _Key, class _Args> | 
|  | 1235 | _LIBCPP_INLINE_VISIBILITY | 
|  | 1236 | pair<iterator, bool> __emplace_unique_key_args(_Key const&, _Args& __args); | 
|  | 1237 | template <class _Key, class _Args> | 
|  | 1238 | _LIBCPP_INLINE_VISIBILITY | 
|  | 1239 | iterator __emplace_hint_unique_key_args(const_iterator, _Key const&, _Args&); | 
| Marshall Clow | 3426a86 | 2016-01-05 19:32:41 | [diff] [blame] | 1240 | #endif | 
|  | 1241 |  | 
| Eric Fiselier | db21506 | 2016-03-31 02:15:15 | [diff] [blame] | 1242 | _LIBCPP_INLINE_VISIBILITY | 
|  | 1243 | pair<iterator, bool> __insert_unique(const __container_value_type& __v) { | 
|  | 1244 | return __emplace_unique_key_args(_NodeTypes::__get_key(__v), __v); | 
|  | 1245 | } | 
|  | 1246 |  | 
|  | 1247 | _LIBCPP_INLINE_VISIBILITY | 
|  | 1248 | iterator __insert_unique(const_iterator __p, const __container_value_type& __v) { | 
|  | 1249 | return __emplace_hint_unique_key_args(__p, _NodeTypes::__get_key(__v), __v); | 
|  | 1250 | } | 
|  | 1251 |  | 
|  | 1252 | #ifdef _LIBCPP_CXX03_LANG | 
|  | 1253 | _LIBCPP_INLINE_VISIBILITY | 
|  | 1254 | iterator __insert_multi(const __container_value_type& __v); | 
|  | 1255 | _LIBCPP_INLINE_VISIBILITY | 
|  | 1256 | iterator __insert_multi(const_iterator __p, const __container_value_type& __v); | 
|  | 1257 | #else | 
|  | 1258 | _LIBCPP_INLINE_VISIBILITY | 
|  | 1259 | pair<iterator, bool> __insert_unique(__container_value_type&& __v) { | 
|  | 1260 | return __emplace_unique_key_args(_NodeTypes::__get_key(__v), _VSTD::move(__v)); | 
|  | 1261 | } | 
|  | 1262 |  | 
|  | 1263 | _LIBCPP_INLINE_VISIBILITY | 
|  | 1264 | iterator __insert_unique(const_iterator __p, __container_value_type&& __v) { | 
|  | 1265 | return __emplace_hint_unique_key_args(__p, _NodeTypes::__get_key(__v), _VSTD::move(__v)); | 
|  | 1266 | } | 
|  | 1267 |  | 
|  | 1268 | template <class _Vp, class = typename enable_if< | 
|  | 1269 | !is_same<typename __unconstref<_Vp>::type, | 
|  | 1270 | __container_value_type | 
|  | 1271 | >::value | 
|  | 1272 | >::type> | 
|  | 1273 | _LIBCPP_INLINE_VISIBILITY | 
|  | 1274 | pair<iterator, bool> __insert_unique(_Vp&& __v) { | 
|  | 1275 | return __emplace_unique(_VSTD::forward<_Vp>(__v)); | 
|  | 1276 | } | 
|  | 1277 |  | 
|  | 1278 | template <class _Vp, class = typename enable_if< | 
|  | 1279 | !is_same<typename __unconstref<_Vp>::type, | 
|  | 1280 | __container_value_type | 
|  | 1281 | >::value | 
|  | 1282 | >::type> | 
|  | 1283 | _LIBCPP_INLINE_VISIBILITY | 
|  | 1284 | iterator __insert_unique(const_iterator __p, _Vp&& __v) { | 
|  | 1285 | return __emplace_hint_unique(__p, _VSTD::forward<_Vp>(__v)); | 
|  | 1286 | } | 
|  | 1287 |  | 
|  | 1288 | _LIBCPP_INLINE_VISIBILITY | 
|  | 1289 | iterator __insert_multi(__container_value_type&& __v) { | 
|  | 1290 | return __emplace_multi(_VSTD::move(__v)); | 
|  | 1291 | } | 
|  | 1292 |  | 
|  | 1293 | _LIBCPP_INLINE_VISIBILITY | 
|  | 1294 | iterator __insert_multi(const_iterator __p, __container_value_type&& __v) { | 
|  | 1295 | return __emplace_hint_multi(__p, _VSTD::move(__v)); | 
|  | 1296 | } | 
|  | 1297 |  | 
|  | 1298 | template <class _Vp> | 
|  | 1299 | _LIBCPP_INLINE_VISIBILITY | 
|  | 1300 | iterator __insert_multi(_Vp&& __v) { | 
|  | 1301 | return __emplace_multi(_VSTD::forward<_Vp>(__v)); | 
|  | 1302 | } | 
|  | 1303 |  | 
|  | 1304 | template <class _Vp> | 
|  | 1305 | _LIBCPP_INLINE_VISIBILITY | 
|  | 1306 | iterator __insert_multi(const_iterator __p, _Vp&& __v) { | 
|  | 1307 | return __emplace_hint_multi(__p, _VSTD::forward<_Vp>(__v)); | 
|  | 1308 | } | 
|  | 1309 |  | 
|  | 1310 | #endif // !_LIBCPP_CXX03_LANG | 
|  | 1311 |  | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 1312 | pair<iterator, bool> __node_insert_unique(__node_pointer __nd); | 
|  | 1313 | iterator __node_insert_unique(const_iterator __p, | 
|  | 1314 | __node_pointer __nd); | 
|  | 1315 |  | 
|  | 1316 | iterator __node_insert_multi(__node_pointer __nd); | 
|  | 1317 | iterator __node_insert_multi(const_iterator __p, __node_pointer __nd); | 
|  | 1318 |  | 
|  | 1319 | iterator erase(const_iterator __p); | 
|  | 1320 | iterator erase(const_iterator __f, const_iterator __l); | 
|  | 1321 | template <class _Key> | 
|  | 1322 | size_type __erase_unique(const _Key& __k); | 
|  | 1323 | template <class _Key> | 
|  | 1324 | size_type __erase_multi(const _Key& __k); | 
|  | 1325 |  | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 1326 | void __insert_node_at(__parent_pointer __parent, | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 1327 | __node_base_pointer& __child, | 
|  | 1328 | __node_base_pointer __new_node); | 
|  | 1329 |  | 
|  | 1330 | template <class _Key> | 
|  | 1331 | iterator find(const _Key& __v); | 
|  | 1332 | template <class _Key> | 
|  | 1333 | const_iterator find(const _Key& __v) const; | 
|  | 1334 |  | 
|  | 1335 | template <class _Key> | 
|  | 1336 | size_type __count_unique(const _Key& __k) const; | 
|  | 1337 | template <class _Key> | 
|  | 1338 | size_type __count_multi(const _Key& __k) const; | 
|  | 1339 |  | 
|  | 1340 | template <class _Key> | 
| Howard Hinnant | 333f50d | 2010-09-21 20:16:37 | [diff] [blame] | 1341 | _LIBCPP_INLINE_VISIBILITY | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 1342 | iterator lower_bound(const _Key& __v) | 
|  | 1343 | {return __lower_bound(__v, __root(), __end_node());} | 
|  | 1344 | template <class _Key> | 
|  | 1345 | iterator __lower_bound(const _Key& __v, | 
|  | 1346 | __node_pointer __root, | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 1347 | __iter_pointer __result); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 1348 | template <class _Key> | 
| Howard Hinnant | 333f50d | 2010-09-21 20:16:37 | [diff] [blame] | 1349 | _LIBCPP_INLINE_VISIBILITY | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 1350 | const_iterator lower_bound(const _Key& __v) const | 
|  | 1351 | {return __lower_bound(__v, __root(), __end_node());} | 
|  | 1352 | template <class _Key> | 
|  | 1353 | const_iterator __lower_bound(const _Key& __v, | 
| Eric Fiselier | 227b47c | 2016-02-20 07:12:17 | [diff] [blame] | 1354 | __node_pointer __root, | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 1355 | __iter_pointer __result) const; | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 1356 | template <class _Key> | 
| Howard Hinnant | 333f50d | 2010-09-21 20:16:37 | [diff] [blame] | 1357 | _LIBCPP_INLINE_VISIBILITY | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 1358 | iterator upper_bound(const _Key& __v) | 
|  | 1359 | {return __upper_bound(__v, __root(), __end_node());} | 
|  | 1360 | template <class _Key> | 
|  | 1361 | iterator __upper_bound(const _Key& __v, | 
|  | 1362 | __node_pointer __root, | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 1363 | __iter_pointer __result); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 1364 | template <class _Key> | 
| Howard Hinnant | 333f50d | 2010-09-21 20:16:37 | [diff] [blame] | 1365 | _LIBCPP_INLINE_VISIBILITY | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 1366 | const_iterator upper_bound(const _Key& __v) const | 
|  | 1367 | {return __upper_bound(__v, __root(), __end_node());} | 
|  | 1368 | template <class _Key> | 
|  | 1369 | const_iterator __upper_bound(const _Key& __v, | 
| Eric Fiselier | 227b47c | 2016-02-20 07:12:17 | [diff] [blame] | 1370 | __node_pointer __root, | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 1371 | __iter_pointer __result) const; | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 1372 | template <class _Key> | 
|  | 1373 | pair<iterator, iterator> | 
|  | 1374 | __equal_range_unique(const _Key& __k); | 
|  | 1375 | template <class _Key> | 
|  | 1376 | pair<const_iterator, const_iterator> | 
|  | 1377 | __equal_range_unique(const _Key& __k) const; | 
|  | 1378 |  | 
|  | 1379 | template <class _Key> | 
|  | 1380 | pair<iterator, iterator> | 
|  | 1381 | __equal_range_multi(const _Key& __k); | 
|  | 1382 | template <class _Key> | 
|  | 1383 | pair<const_iterator, const_iterator> | 
|  | 1384 | __equal_range_multi(const _Key& __k) const; | 
|  | 1385 |  | 
| Howard Hinnant | 9996844 | 2011-11-29 18:15:50 | [diff] [blame] | 1386 | typedef __tree_node_destructor<__node_allocator> _Dp; | 
|  | 1387 | typedef unique_ptr<__node, _Dp> __node_holder; | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 1388 |  | 
| Howard Hinnant | 8b53768 | 2011-06-04 17:10:24 | [diff] [blame] | 1389 | __node_holder remove(const_iterator __p) _NOEXCEPT; | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 1390 | private: | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 1391 | __node_base_pointer& | 
|  | 1392 | __find_leaf_low(__parent_pointer& __parent, const key_type& __v); | 
|  | 1393 | __node_base_pointer& | 
|  | 1394 | __find_leaf_high(__parent_pointer& __parent, const key_type& __v); | 
|  | 1395 | __node_base_pointer& | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 1396 | __find_leaf(const_iterator __hint, | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 1397 | __parent_pointer& __parent, const key_type& __v); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 1398 | template <class _Key> | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 1399 | __node_base_pointer& | 
|  | 1400 | __find_equal(__parent_pointer& __parent, const _Key& __v); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 1401 | template <class _Key> | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 1402 | __node_base_pointer& | 
|  | 1403 | __find_equal(const_iterator __hint, __parent_pointer& __parent, | 
|  | 1404 | __node_base_pointer& __dummy, | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 1405 | const _Key& __v); | 
|  | 1406 |  | 
| Eric Fiselier | db21506 | 2016-03-31 02:15:15 | [diff] [blame] | 1407 | #ifndef _LIBCPP_CXX03_LANG | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 1408 | template <class ..._Args> | 
| Eric Fiselier | db21506 | 2016-03-31 02:15:15 | [diff] [blame] | 1409 | __node_holder __construct_node(_Args&& ...__args); | 
|  | 1410 | #else | 
|  | 1411 | __node_holder __construct_node(const __container_value_type& __v); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 1412 | #endif | 
|  | 1413 |  | 
| Howard Hinnant | 7686add | 2011-06-04 14:31:57 | [diff] [blame] | 1414 | void destroy(__node_pointer __nd) _NOEXCEPT; | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 1415 |  | 
| Howard Hinnant | 333f50d | 2010-09-21 20:16:37 | [diff] [blame] | 1416 | _LIBCPP_INLINE_VISIBILITY | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 1417 | void __copy_assign_alloc(const __tree& __t) | 
|  | 1418 | {__copy_assign_alloc(__t, integral_constant<bool, | 
|  | 1419 | __node_traits::propagate_on_container_copy_assignment::value>());} | 
|  | 1420 |  | 
| Howard Hinnant | 333f50d | 2010-09-21 20:16:37 | [diff] [blame] | 1421 | _LIBCPP_INLINE_VISIBILITY | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 1422 | void __copy_assign_alloc(const __tree& __t, true_type) | 
| Marshall Clow | 546498c | 2016-08-17 23:24:02 | [diff] [blame] | 1423 | { | 
|  | 1424 | if (__node_alloc() != __t.__node_alloc()) | 
|  | 1425 | clear(); | 
|  | 1426 | __node_alloc() = __t.__node_alloc(); | 
|  | 1427 | } | 
| Howard Hinnant | 333f50d | 2010-09-21 20:16:37 | [diff] [blame] | 1428 | _LIBCPP_INLINE_VISIBILITY | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 1429 | void __copy_assign_alloc(const __tree& __t, false_type) {} | 
|  | 1430 |  | 
|  | 1431 | void __move_assign(__tree& __t, false_type); | 
| Howard Hinnant | 7686add | 2011-06-04 14:31:57 | [diff] [blame] | 1432 | void __move_assign(__tree& __t, true_type) | 
|  | 1433 | _NOEXCEPT_(is_nothrow_move_assignable<value_compare>::value && | 
|  | 1434 | is_nothrow_move_assignable<__node_allocator>::value); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 1435 |  | 
| Howard Hinnant | 333f50d | 2010-09-21 20:16:37 | [diff] [blame] | 1436 | _LIBCPP_INLINE_VISIBILITY | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 1437 | void __move_assign_alloc(__tree& __t) | 
| Howard Hinnant | 7686add | 2011-06-04 14:31:57 | [diff] [blame] | 1438 | _NOEXCEPT_( | 
|  | 1439 | !__node_traits::propagate_on_container_move_assignment::value || | 
|  | 1440 | is_nothrow_move_assignable<__node_allocator>::value) | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 1441 | {__move_assign_alloc(__t, integral_constant<bool, | 
|  | 1442 | __node_traits::propagate_on_container_move_assignment::value>());} | 
|  | 1443 |  | 
| Howard Hinnant | 333f50d | 2010-09-21 20:16:37 | [diff] [blame] | 1444 | _LIBCPP_INLINE_VISIBILITY | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 1445 | void __move_assign_alloc(__tree& __t, true_type) | 
| Howard Hinnant | 7686add | 2011-06-04 14:31:57 | [diff] [blame] | 1446 | _NOEXCEPT_(is_nothrow_move_assignable<__node_allocator>::value) | 
| Howard Hinnant | 0949eed | 2011-06-30 21:18:19 | [diff] [blame] | 1447 | {__node_alloc() = _VSTD::move(__t.__node_alloc());} | 
| Howard Hinnant | 333f50d | 2010-09-21 20:16:37 | [diff] [blame] | 1448 | _LIBCPP_INLINE_VISIBILITY | 
| Howard Hinnant | 7686add | 2011-06-04 14:31:57 | [diff] [blame] | 1449 | void __move_assign_alloc(__tree& __t, false_type) _NOEXCEPT {} | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 1450 |  | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 1451 | __node_pointer __detach(); | 
|  | 1452 | static __node_pointer __detach(__node_pointer); | 
| Howard Hinnant | 70342b9 | 2013-06-19 21:29:40 | [diff] [blame] | 1453 |  | 
| Howard Hinnant | 0f678bd | 2013-08-12 18:38:34 | [diff] [blame] | 1454 | template <class, class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY map; | 
|  | 1455 | template <class, class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY multimap; | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 1456 | }; | 
|  | 1457 |  | 
|  | 1458 | template <class _Tp, class _Compare, class _Allocator> | 
|  | 1459 | __tree<_Tp, _Compare, _Allocator>::__tree(const value_compare& __comp) | 
| Howard Hinnant | 7686add | 2011-06-04 14:31:57 | [diff] [blame] | 1460 | _NOEXCEPT_( | 
|  | 1461 | is_nothrow_default_constructible<__node_allocator>::value && | 
|  | 1462 | is_nothrow_copy_constructible<value_compare>::value) | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 1463 | : __pair3_(0, __comp) | 
|  | 1464 | { | 
|  | 1465 | __begin_node() = __end_node(); | 
|  | 1466 | } | 
|  | 1467 |  | 
|  | 1468 | template <class _Tp, class _Compare, class _Allocator> | 
|  | 1469 | __tree<_Tp, _Compare, _Allocator>::__tree(const allocator_type& __a) | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 1470 | : __begin_node_(__iter_pointer()), | 
| Eric Fiselier | 02bb4bd | 2015-07-18 23:56:04 | [diff] [blame] | 1471 | __pair1_(__node_allocator(__a)), | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 1472 | __pair3_(0) | 
|  | 1473 | { | 
|  | 1474 | __begin_node() = __end_node(); | 
|  | 1475 | } | 
|  | 1476 |  | 
|  | 1477 | template <class _Tp, class _Compare, class _Allocator> | 
|  | 1478 | __tree<_Tp, _Compare, _Allocator>::__tree(const value_compare& __comp, | 
|  | 1479 | const allocator_type& __a) | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 1480 | : __begin_node_(__iter_pointer()), | 
| Eric Fiselier | 02bb4bd | 2015-07-18 23:56:04 | [diff] [blame] | 1481 | __pair1_(__node_allocator(__a)), | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 1482 | __pair3_(0, __comp) | 
|  | 1483 | { | 
|  | 1484 | __begin_node() = __end_node(); | 
|  | 1485 | } | 
|  | 1486 |  | 
|  | 1487 | // Precondition: size() != 0 | 
|  | 1488 | template <class _Tp, class _Compare, class _Allocator> | 
|  | 1489 | typename __tree<_Tp, _Compare, _Allocator>::__node_pointer | 
|  | 1490 | __tree<_Tp, _Compare, _Allocator>::__detach() | 
|  | 1491 | { | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 1492 | __node_pointer __cache = static_cast<__node_pointer>(__begin_node()); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 1493 | __begin_node() = __end_node(); | 
|  | 1494 | __end_node()->__left_->__parent_ = nullptr; | 
|  | 1495 | __end_node()->__left_ = nullptr; | 
|  | 1496 | size() = 0; | 
|  | 1497 | // __cache->__left_ == nullptr | 
|  | 1498 | if (__cache->__right_ != nullptr) | 
|  | 1499 | __cache = static_cast<__node_pointer>(__cache->__right_); | 
|  | 1500 | // __cache->__left_ == nullptr | 
|  | 1501 | // __cache->__right_ == nullptr | 
|  | 1502 | return __cache; | 
|  | 1503 | } | 
|  | 1504 |  | 
|  | 1505 | // Precondition: __cache != nullptr | 
|  | 1506 | // __cache->left_ == nullptr | 
|  | 1507 | // __cache->right_ == nullptr | 
|  | 1508 | // This is no longer a red-black tree | 
|  | 1509 | template <class _Tp, class _Compare, class _Allocator> | 
|  | 1510 | typename __tree<_Tp, _Compare, _Allocator>::__node_pointer | 
|  | 1511 | __tree<_Tp, _Compare, _Allocator>::__detach(__node_pointer __cache) | 
|  | 1512 | { | 
|  | 1513 | if (__cache->__parent_ == nullptr) | 
|  | 1514 | return nullptr; | 
| Howard Hinnant | 70342b9 | 2013-06-19 21:29:40 | [diff] [blame] | 1515 | if (__tree_is_left_child(static_cast<__node_base_pointer>(__cache))) | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 1516 | { | 
|  | 1517 | __cache->__parent_->__left_ = nullptr; | 
|  | 1518 | __cache = static_cast<__node_pointer>(__cache->__parent_); | 
|  | 1519 | if (__cache->__right_ == nullptr) | 
|  | 1520 | return __cache; | 
|  | 1521 | return static_cast<__node_pointer>(__tree_leaf(__cache->__right_)); | 
|  | 1522 | } | 
|  | 1523 | // __cache is right child | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 1524 | __cache->__parent_unsafe()->__right_ = nullptr; | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 1525 | __cache = static_cast<__node_pointer>(__cache->__parent_); | 
|  | 1526 | if (__cache->__left_ == nullptr) | 
|  | 1527 | return __cache; | 
|  | 1528 | return static_cast<__node_pointer>(__tree_leaf(__cache->__left_)); | 
|  | 1529 | } | 
|  | 1530 |  | 
|  | 1531 | template <class _Tp, class _Compare, class _Allocator> | 
|  | 1532 | __tree<_Tp, _Compare, _Allocator>& | 
|  | 1533 | __tree<_Tp, _Compare, _Allocator>::operator=(const __tree& __t) | 
|  | 1534 | { | 
|  | 1535 | if (this != &__t) | 
|  | 1536 | { | 
|  | 1537 | value_comp() = __t.value_comp(); | 
|  | 1538 | __copy_assign_alloc(__t); | 
|  | 1539 | __assign_multi(__t.begin(), __t.end()); | 
|  | 1540 | } | 
|  | 1541 | return *this; | 
|  | 1542 | } | 
|  | 1543 |  | 
|  | 1544 | template <class _Tp, class _Compare, class _Allocator> | 
|  | 1545 | template <class _InputIterator> | 
|  | 1546 | void | 
|  | 1547 | __tree<_Tp, _Compare, _Allocator>::__assign_unique(_InputIterator __first, _InputIterator __last) | 
|  | 1548 | { | 
| Eric Fiselier | db21506 | 2016-03-31 02:15:15 | [diff] [blame] | 1549 | typedef iterator_traits<_InputIterator> _ITraits; | 
|  | 1550 | typedef typename _ITraits::value_type _ItValueType; | 
|  | 1551 | static_assert((is_same<_ItValueType, __container_value_type>::value), | 
|  | 1552 | "__assign_unique may only be called with the containers value type"); | 
|  | 1553 |  | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 1554 | if (size() != 0) | 
|  | 1555 | { | 
|  | 1556 | __node_pointer __cache = __detach(); | 
| Howard Hinnant | d444470 | 2010-08-11 17:04:31 | [diff] [blame] | 1557 | #ifndef _LIBCPP_NO_EXCEPTIONS | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 1558 | try | 
|  | 1559 | { | 
| Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 | [diff] [blame] | 1560 | #endif // _LIBCPP_NO_EXCEPTIONS | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 1561 | for (; __cache != nullptr && __first != __last; ++__first) | 
|  | 1562 | { | 
|  | 1563 | __cache->__value_ = *__first; | 
|  | 1564 | __node_pointer __next = __detach(__cache); | 
|  | 1565 | __node_insert_unique(__cache); | 
|  | 1566 | __cache = __next; | 
|  | 1567 | } | 
| Howard Hinnant | d444470 | 2010-08-11 17:04:31 | [diff] [blame] | 1568 | #ifndef _LIBCPP_NO_EXCEPTIONS | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 1569 | } | 
|  | 1570 | catch (...) | 
|  | 1571 | { | 
|  | 1572 | while (__cache->__parent_ != nullptr) | 
|  | 1573 | __cache = static_cast<__node_pointer>(__cache->__parent_); | 
|  | 1574 | destroy(__cache); | 
|  | 1575 | throw; | 
|  | 1576 | } | 
| Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 | [diff] [blame] | 1577 | #endif // _LIBCPP_NO_EXCEPTIONS | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 1578 | if (__cache != nullptr) | 
|  | 1579 | { | 
|  | 1580 | while (__cache->__parent_ != nullptr) | 
|  | 1581 | __cache = static_cast<__node_pointer>(__cache->__parent_); | 
|  | 1582 | destroy(__cache); | 
|  | 1583 | } | 
|  | 1584 | } | 
|  | 1585 | for (; __first != __last; ++__first) | 
|  | 1586 | __insert_unique(*__first); | 
|  | 1587 | } | 
|  | 1588 |  | 
|  | 1589 | template <class _Tp, class _Compare, class _Allocator> | 
|  | 1590 | template <class _InputIterator> | 
|  | 1591 | void | 
|  | 1592 | __tree<_Tp, _Compare, _Allocator>::__assign_multi(_InputIterator __first, _InputIterator __last) | 
|  | 1593 | { | 
| Eric Fiselier | db21506 | 2016-03-31 02:15:15 | [diff] [blame] | 1594 | typedef iterator_traits<_InputIterator> _ITraits; | 
|  | 1595 | typedef typename _ITraits::value_type _ItValueType; | 
|  | 1596 | static_assert((is_same<_ItValueType, __container_value_type>::value || | 
|  | 1597 | is_same<_ItValueType, __node_value_type>::value), | 
|  | 1598 | "__assign_multi may only be called with the containers value type" | 
|  | 1599 | " or the nodes value type"); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 1600 | if (size() != 0) | 
|  | 1601 | { | 
|  | 1602 | __node_pointer __cache = __detach(); | 
| Howard Hinnant | d444470 | 2010-08-11 17:04:31 | [diff] [blame] | 1603 | #ifndef _LIBCPP_NO_EXCEPTIONS | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 1604 | try | 
|  | 1605 | { | 
| Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 | [diff] [blame] | 1606 | #endif // _LIBCPP_NO_EXCEPTIONS | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 1607 | for (; __cache != nullptr && __first != __last; ++__first) | 
|  | 1608 | { | 
|  | 1609 | __cache->__value_ = *__first; | 
|  | 1610 | __node_pointer __next = __detach(__cache); | 
|  | 1611 | __node_insert_multi(__cache); | 
|  | 1612 | __cache = __next; | 
|  | 1613 | } | 
| Howard Hinnant | d444470 | 2010-08-11 17:04:31 | [diff] [blame] | 1614 | #ifndef _LIBCPP_NO_EXCEPTIONS | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 1615 | } | 
|  | 1616 | catch (...) | 
|  | 1617 | { | 
|  | 1618 | while (__cache->__parent_ != nullptr) | 
|  | 1619 | __cache = static_cast<__node_pointer>(__cache->__parent_); | 
|  | 1620 | destroy(__cache); | 
|  | 1621 | throw; | 
|  | 1622 | } | 
| Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 | [diff] [blame] | 1623 | #endif // _LIBCPP_NO_EXCEPTIONS | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 1624 | if (__cache != nullptr) | 
|  | 1625 | { | 
|  | 1626 | while (__cache->__parent_ != nullptr) | 
|  | 1627 | __cache = static_cast<__node_pointer>(__cache->__parent_); | 
|  | 1628 | destroy(__cache); | 
|  | 1629 | } | 
|  | 1630 | } | 
|  | 1631 | for (; __first != __last; ++__first) | 
| Eric Fiselier | db21506 | 2016-03-31 02:15:15 | [diff] [blame] | 1632 | __insert_multi(_NodeTypes::__get_value(*__first)); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 1633 | } | 
|  | 1634 |  | 
|  | 1635 | template <class _Tp, class _Compare, class _Allocator> | 
|  | 1636 | __tree<_Tp, _Compare, _Allocator>::__tree(const __tree& __t) | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 1637 | : __begin_node_(__iter_pointer()), | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 1638 | __pair1_(__node_traits::select_on_container_copy_construction(__t.__node_alloc())), | 
|  | 1639 | __pair3_(0, __t.value_comp()) | 
|  | 1640 | { | 
|  | 1641 | __begin_node() = __end_node(); | 
|  | 1642 | } | 
|  | 1643 |  | 
| Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 | [diff] [blame] | 1644 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 1645 |  | 
|  | 1646 | template <class _Tp, class _Compare, class _Allocator> | 
|  | 1647 | __tree<_Tp, _Compare, _Allocator>::__tree(__tree&& __t) | 
| Howard Hinnant | 7686add | 2011-06-04 14:31:57 | [diff] [blame] | 1648 | _NOEXCEPT_( | 
|  | 1649 | is_nothrow_move_constructible<__node_allocator>::value && | 
|  | 1650 | is_nothrow_move_constructible<value_compare>::value) | 
| Howard Hinnant | 0949eed | 2011-06-30 21:18:19 | [diff] [blame] | 1651 | : __begin_node_(_VSTD::move(__t.__begin_node_)), | 
|  | 1652 | __pair1_(_VSTD::move(__t.__pair1_)), | 
|  | 1653 | __pair3_(_VSTD::move(__t.__pair3_)) | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 1654 | { | 
|  | 1655 | if (size() == 0) | 
|  | 1656 | __begin_node() = __end_node(); | 
|  | 1657 | else | 
|  | 1658 | { | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 1659 | __end_node()->__left_->__parent_ = static_cast<__parent_pointer>(__end_node()); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 1660 | __t.__begin_node() = __t.__end_node(); | 
|  | 1661 | __t.__end_node()->__left_ = nullptr; | 
|  | 1662 | __t.size() = 0; | 
|  | 1663 | } | 
|  | 1664 | } | 
|  | 1665 |  | 
|  | 1666 | template <class _Tp, class _Compare, class _Allocator> | 
|  | 1667 | __tree<_Tp, _Compare, _Allocator>::__tree(__tree&& __t, const allocator_type& __a) | 
|  | 1668 | : __pair1_(__node_allocator(__a)), | 
| Howard Hinnant | 0949eed | 2011-06-30 21:18:19 | [diff] [blame] | 1669 | __pair3_(0, _VSTD::move(__t.value_comp())) | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 1670 | { | 
|  | 1671 | if (__a == __t.__alloc()) | 
|  | 1672 | { | 
|  | 1673 | if (__t.size() == 0) | 
|  | 1674 | __begin_node() = __end_node(); | 
|  | 1675 | else | 
|  | 1676 | { | 
|  | 1677 | __begin_node() = __t.__begin_node(); | 
|  | 1678 | __end_node()->__left_ = __t.__end_node()->__left_; | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 1679 | __end_node()->__left_->__parent_ = static_cast<__parent_pointer>(__end_node()); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 1680 | size() = __t.size(); | 
|  | 1681 | __t.__begin_node() = __t.__end_node(); | 
|  | 1682 | __t.__end_node()->__left_ = nullptr; | 
|  | 1683 | __t.size() = 0; | 
|  | 1684 | } | 
|  | 1685 | } | 
|  | 1686 | else | 
|  | 1687 | { | 
|  | 1688 | __begin_node() = __end_node(); | 
|  | 1689 | } | 
|  | 1690 | } | 
|  | 1691 |  | 
|  | 1692 | template <class _Tp, class _Compare, class _Allocator> | 
|  | 1693 | void | 
|  | 1694 | __tree<_Tp, _Compare, _Allocator>::__move_assign(__tree& __t, true_type) | 
| Howard Hinnant | 7686add | 2011-06-04 14:31:57 | [diff] [blame] | 1695 | _NOEXCEPT_(is_nothrow_move_assignable<value_compare>::value && | 
|  | 1696 | is_nothrow_move_assignable<__node_allocator>::value) | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 1697 | { | 
|  | 1698 | destroy(static_cast<__node_pointer>(__end_node()->__left_)); | 
|  | 1699 | __begin_node_ = __t.__begin_node_; | 
|  | 1700 | __pair1_.first() = __t.__pair1_.first(); | 
|  | 1701 | __move_assign_alloc(__t); | 
| Howard Hinnant | 0949eed | 2011-06-30 21:18:19 | [diff] [blame] | 1702 | __pair3_ = _VSTD::move(__t.__pair3_); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 1703 | if (size() == 0) | 
|  | 1704 | __begin_node() = __end_node(); | 
|  | 1705 | else | 
|  | 1706 | { | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 1707 | __end_node()->__left_->__parent_ = static_cast<__parent_pointer>(__end_node()); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 1708 | __t.__begin_node() = __t.__end_node(); | 
|  | 1709 | __t.__end_node()->__left_ = nullptr; | 
|  | 1710 | __t.size() = 0; | 
|  | 1711 | } | 
|  | 1712 | } | 
|  | 1713 |  | 
|  | 1714 | template <class _Tp, class _Compare, class _Allocator> | 
|  | 1715 | void | 
|  | 1716 | __tree<_Tp, _Compare, _Allocator>::__move_assign(__tree& __t, false_type) | 
|  | 1717 | { | 
|  | 1718 | if (__node_alloc() == __t.__node_alloc()) | 
|  | 1719 | __move_assign(__t, true_type()); | 
|  | 1720 | else | 
|  | 1721 | { | 
| Howard Hinnant | 0949eed | 2011-06-30 21:18:19 | [diff] [blame] | 1722 | value_comp() = _VSTD::move(__t.value_comp()); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 1723 | const_iterator __e = end(); | 
|  | 1724 | if (size() != 0) | 
|  | 1725 | { | 
|  | 1726 | __node_pointer __cache = __detach(); | 
| Howard Hinnant | d444470 | 2010-08-11 17:04:31 | [diff] [blame] | 1727 | #ifndef _LIBCPP_NO_EXCEPTIONS | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 1728 | try | 
|  | 1729 | { | 
| Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 | [diff] [blame] | 1730 | #endif // _LIBCPP_NO_EXCEPTIONS | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 1731 | while (__cache != nullptr && __t.size() != 0) | 
|  | 1732 | { | 
| Howard Hinnant | 0949eed | 2011-06-30 21:18:19 | [diff] [blame] | 1733 | __cache->__value_ = _VSTD::move(__t.remove(__t.begin())->__value_); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 1734 | __node_pointer __next = __detach(__cache); | 
|  | 1735 | __node_insert_multi(__cache); | 
|  | 1736 | __cache = __next; | 
|  | 1737 | } | 
| Howard Hinnant | d444470 | 2010-08-11 17:04:31 | [diff] [blame] | 1738 | #ifndef _LIBCPP_NO_EXCEPTIONS | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 1739 | } | 
|  | 1740 | catch (...) | 
|  | 1741 | { | 
|  | 1742 | while (__cache->__parent_ != nullptr) | 
|  | 1743 | __cache = static_cast<__node_pointer>(__cache->__parent_); | 
|  | 1744 | destroy(__cache); | 
|  | 1745 | throw; | 
|  | 1746 | } | 
| Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 | [diff] [blame] | 1747 | #endif // _LIBCPP_NO_EXCEPTIONS | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 1748 | if (__cache != nullptr) | 
|  | 1749 | { | 
|  | 1750 | while (__cache->__parent_ != nullptr) | 
|  | 1751 | __cache = static_cast<__node_pointer>(__cache->__parent_); | 
|  | 1752 | destroy(__cache); | 
|  | 1753 | } | 
|  | 1754 | } | 
|  | 1755 | while (__t.size() != 0) | 
| Eric Fiselier | db21506 | 2016-03-31 02:15:15 | [diff] [blame] | 1756 | __insert_multi(__e, _NodeTypes::__move(__t.remove(__t.begin())->__value_)); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 1757 | } | 
|  | 1758 | } | 
|  | 1759 |  | 
|  | 1760 | template <class _Tp, class _Compare, class _Allocator> | 
|  | 1761 | __tree<_Tp, _Compare, _Allocator>& | 
|  | 1762 | __tree<_Tp, _Compare, _Allocator>::operator=(__tree&& __t) | 
| Howard Hinnant | 7686add | 2011-06-04 14:31:57 | [diff] [blame] | 1763 | _NOEXCEPT_( | 
|  | 1764 | __node_traits::propagate_on_container_move_assignment::value && | 
|  | 1765 | is_nothrow_move_assignable<value_compare>::value && | 
|  | 1766 | is_nothrow_move_assignable<__node_allocator>::value) | 
|  | 1767 |  | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 1768 | { | 
|  | 1769 | __move_assign(__t, integral_constant<bool, | 
|  | 1770 | __node_traits::propagate_on_container_move_assignment::value>()); | 
|  | 1771 | return *this; | 
|  | 1772 | } | 
|  | 1773 |  | 
| Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 | [diff] [blame] | 1774 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 1775 |  | 
|  | 1776 | template <class _Tp, class _Compare, class _Allocator> | 
|  | 1777 | __tree<_Tp, _Compare, _Allocator>::~__tree() | 
|  | 1778 | { | 
| Marshall Clow | 1a93312 | 2016-06-30 22:05:45 | [diff] [blame] | 1779 | static_assert((is_copy_constructible<value_compare>::value), | 
|  | 1780 | "Comparator must be copy-constructible."); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 1781 | destroy(__root()); | 
|  | 1782 | } | 
|  | 1783 |  | 
|  | 1784 | template <class _Tp, class _Compare, class _Allocator> | 
|  | 1785 | void | 
| Howard Hinnant | 7686add | 2011-06-04 14:31:57 | [diff] [blame] | 1786 | __tree<_Tp, _Compare, _Allocator>::destroy(__node_pointer __nd) _NOEXCEPT | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 1787 | { | 
|  | 1788 | if (__nd != nullptr) | 
|  | 1789 | { | 
|  | 1790 | destroy(static_cast<__node_pointer>(__nd->__left_)); | 
|  | 1791 | destroy(static_cast<__node_pointer>(__nd->__right_)); | 
|  | 1792 | __node_allocator& __na = __node_alloc(); | 
| Eric Fiselier | db21506 | 2016-03-31 02:15:15 | [diff] [blame] | 1793 | __node_traits::destroy(__na, _NodeTypes::__get_ptr(__nd->__value_)); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 1794 | __node_traits::deallocate(__na, __nd, 1); | 
|  | 1795 | } | 
|  | 1796 | } | 
|  | 1797 |  | 
|  | 1798 | template <class _Tp, class _Compare, class _Allocator> | 
|  | 1799 | void | 
|  | 1800 | __tree<_Tp, _Compare, _Allocator>::swap(__tree& __t) | 
| Dimitry Andric | 1421cf0 | 2016-08-27 19:32:03 | [diff] [blame] | 1801 | #if _LIBCPP_STD_VER <= 11 | 
| Marshall Clow | 7d914d1 | 2015-07-13 20:04:56 | [diff] [blame] | 1802 | _NOEXCEPT_( | 
|  | 1803 | __is_nothrow_swappable<value_compare>::value | 
| Marshall Clow | 7d914d1 | 2015-07-13 20:04:56 | [diff] [blame] | 1804 | && (!__node_traits::propagate_on_container_swap::value || | 
|  | 1805 | __is_nothrow_swappable<__node_allocator>::value) | 
| Marshall Clow | 7d914d1 | 2015-07-13 20:04:56 | [diff] [blame] | 1806 | ) | 
| Dimitry Andric | 1421cf0 | 2016-08-27 19:32:03 | [diff] [blame] | 1807 | #else | 
|  | 1808 | _NOEXCEPT_(__is_nothrow_swappable<value_compare>::value) | 
|  | 1809 | #endif | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 1810 | { | 
| Howard Hinnant | 0949eed | 2011-06-30 21:18:19 | [diff] [blame] | 1811 | using _VSTD::swap; | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 1812 | swap(__begin_node_, __t.__begin_node_); | 
|  | 1813 | swap(__pair1_.first(), __t.__pair1_.first()); | 
| Marshall Clow | 7d914d1 | 2015-07-13 20:04:56 | [diff] [blame] | 1814 | __swap_allocator(__node_alloc(), __t.__node_alloc()); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 1815 | __pair3_.swap(__t.__pair3_); | 
|  | 1816 | if (size() == 0) | 
|  | 1817 | __begin_node() = __end_node(); | 
|  | 1818 | else | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 1819 | __end_node()->__left_->__parent_ = static_cast<__parent_pointer>(__end_node()); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 1820 | if (__t.size() == 0) | 
|  | 1821 | __t.__begin_node() = __t.__end_node(); | 
|  | 1822 | else | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 1823 | __t.__end_node()->__left_->__parent_ = static_cast<__parent_pointer>(__t.__end_node()); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 1824 | } | 
|  | 1825 |  | 
|  | 1826 | template <class _Tp, class _Compare, class _Allocator> | 
|  | 1827 | void | 
| Howard Hinnant | 7686add | 2011-06-04 14:31:57 | [diff] [blame] | 1828 | __tree<_Tp, _Compare, _Allocator>::clear() _NOEXCEPT | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 1829 | { | 
|  | 1830 | destroy(__root()); | 
|  | 1831 | size() = 0; | 
|  | 1832 | __begin_node() = __end_node(); | 
|  | 1833 | __end_node()->__left_ = nullptr; | 
|  | 1834 | } | 
|  | 1835 |  | 
|  | 1836 | // Find lower_bound place to insert | 
|  | 1837 | // Set __parent to parent of null leaf | 
|  | 1838 | // Return reference to null leaf | 
|  | 1839 | template <class _Tp, class _Compare, class _Allocator> | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 1840 | typename __tree<_Tp, _Compare, _Allocator>::__node_base_pointer& | 
|  | 1841 | __tree<_Tp, _Compare, _Allocator>::__find_leaf_low(__parent_pointer& __parent, | 
| Eric Fiselier | db21506 | 2016-03-31 02:15:15 | [diff] [blame] | 1842 | const key_type& __v) | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 1843 | { | 
|  | 1844 | __node_pointer __nd = __root(); | 
|  | 1845 | if (__nd != nullptr) | 
|  | 1846 | { | 
|  | 1847 | while (true) | 
|  | 1848 | { | 
|  | 1849 | if (value_comp()(__nd->__value_, __v)) | 
|  | 1850 | { | 
|  | 1851 | if (__nd->__right_ != nullptr) | 
|  | 1852 | __nd = static_cast<__node_pointer>(__nd->__right_); | 
|  | 1853 | else | 
|  | 1854 | { | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 1855 | __parent = static_cast<__parent_pointer>(__nd); | 
|  | 1856 | return __nd->__right_; | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 1857 | } | 
|  | 1858 | } | 
|  | 1859 | else | 
|  | 1860 | { | 
|  | 1861 | if (__nd->__left_ != nullptr) | 
|  | 1862 | __nd = static_cast<__node_pointer>(__nd->__left_); | 
|  | 1863 | else | 
|  | 1864 | { | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 1865 | __parent = static_cast<__parent_pointer>(__nd); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 1866 | return __parent->__left_; | 
|  | 1867 | } | 
|  | 1868 | } | 
|  | 1869 | } | 
|  | 1870 | } | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 1871 | __parent = static_cast<__parent_pointer>(__end_node()); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 1872 | return __parent->__left_; | 
|  | 1873 | } | 
|  | 1874 |  | 
|  | 1875 | // Find upper_bound place to insert | 
|  | 1876 | // Set __parent to parent of null leaf | 
|  | 1877 | // Return reference to null leaf | 
|  | 1878 | template <class _Tp, class _Compare, class _Allocator> | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 1879 | typename __tree<_Tp, _Compare, _Allocator>::__node_base_pointer& | 
|  | 1880 | __tree<_Tp, _Compare, _Allocator>::__find_leaf_high(__parent_pointer& __parent, | 
| Eric Fiselier | db21506 | 2016-03-31 02:15:15 | [diff] [blame] | 1881 | const key_type& __v) | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 1882 | { | 
|  | 1883 | __node_pointer __nd = __root(); | 
|  | 1884 | if (__nd != nullptr) | 
|  | 1885 | { | 
|  | 1886 | while (true) | 
|  | 1887 | { | 
|  | 1888 | if (value_comp()(__v, __nd->__value_)) | 
|  | 1889 | { | 
|  | 1890 | if (__nd->__left_ != nullptr) | 
|  | 1891 | __nd = static_cast<__node_pointer>(__nd->__left_); | 
|  | 1892 | else | 
|  | 1893 | { | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 1894 | __parent = static_cast<__parent_pointer>(__nd); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 1895 | return __parent->__left_; | 
|  | 1896 | } | 
|  | 1897 | } | 
|  | 1898 | else | 
|  | 1899 | { | 
|  | 1900 | if (__nd->__right_ != nullptr) | 
|  | 1901 | __nd = static_cast<__node_pointer>(__nd->__right_); | 
|  | 1902 | else | 
|  | 1903 | { | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 1904 | __parent = static_cast<__parent_pointer>(__nd); | 
|  | 1905 | return __nd->__right_; | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 1906 | } | 
|  | 1907 | } | 
|  | 1908 | } | 
|  | 1909 | } | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 1910 | __parent = static_cast<__parent_pointer>(__end_node()); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 1911 | return __parent->__left_; | 
|  | 1912 | } | 
|  | 1913 |  | 
|  | 1914 | // Find leaf place to insert closest to __hint | 
|  | 1915 | // First check prior to __hint. | 
|  | 1916 | // Next check after __hint. | 
|  | 1917 | // Next do O(log N) search. | 
|  | 1918 | // Set __parent to parent of null leaf | 
|  | 1919 | // Return reference to null leaf | 
|  | 1920 | template <class _Tp, class _Compare, class _Allocator> | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 1921 | typename __tree<_Tp, _Compare, _Allocator>::__node_base_pointer& | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 1922 | __tree<_Tp, _Compare, _Allocator>::__find_leaf(const_iterator __hint, | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 1923 | __parent_pointer& __parent, | 
| Eric Fiselier | db21506 | 2016-03-31 02:15:15 | [diff] [blame] | 1924 | const key_type& __v) | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 1925 | { | 
|  | 1926 | if (__hint == end() || !value_comp()(*__hint, __v)) // check before | 
|  | 1927 | { | 
|  | 1928 | // __v <= *__hint | 
|  | 1929 | const_iterator __prior = __hint; | 
|  | 1930 | if (__prior == begin() || !value_comp()(__v, *--__prior)) | 
|  | 1931 | { | 
|  | 1932 | // *prev(__hint) <= __v <= *__hint | 
|  | 1933 | if (__hint.__ptr_->__left_ == nullptr) | 
|  | 1934 | { | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 1935 | __parent = static_cast<__parent_pointer>(__hint.__ptr_); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 1936 | return __parent->__left_; | 
|  | 1937 | } | 
|  | 1938 | else | 
|  | 1939 | { | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 1940 | __parent = static_cast<__parent_pointer>(__prior.__ptr_); | 
|  | 1941 | return static_cast<__node_base_pointer>(__prior.__ptr_)->__right_; | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 1942 | } | 
|  | 1943 | } | 
|  | 1944 | // __v < *prev(__hint) | 
|  | 1945 | return __find_leaf_high(__parent, __v); | 
|  | 1946 | } | 
|  | 1947 | // else __v > *__hint | 
|  | 1948 | return __find_leaf_low(__parent, __v); | 
|  | 1949 | } | 
|  | 1950 |  | 
|  | 1951 | // Find place to insert if __v doesn't exist | 
|  | 1952 | // Set __parent to parent of null leaf | 
|  | 1953 | // Return reference to null leaf | 
|  | 1954 | // If __v exists, set parent to node of __v and return reference to node of __v | 
|  | 1955 | template <class _Tp, class _Compare, class _Allocator> | 
|  | 1956 | template <class _Key> | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 1957 | typename __tree<_Tp, _Compare, _Allocator>::__node_base_pointer& | 
|  | 1958 | __tree<_Tp, _Compare, _Allocator>::__find_equal(__parent_pointer& __parent, | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 1959 | const _Key& __v) | 
|  | 1960 | { | 
|  | 1961 | __node_pointer __nd = __root(); | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 1962 | __node_base_pointer* __nd_ptr = __root_ptr(); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 1963 | if (__nd != nullptr) | 
|  | 1964 | { | 
|  | 1965 | while (true) | 
|  | 1966 | { | 
|  | 1967 | if (value_comp()(__v, __nd->__value_)) | 
|  | 1968 | { | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 1969 | if (__nd->__left_ != nullptr) { | 
|  | 1970 | __nd_ptr = _VSTD::addressof(__nd->__left_); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 1971 | __nd = static_cast<__node_pointer>(__nd->__left_); | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 1972 | } else { | 
|  | 1973 | __parent = static_cast<__parent_pointer>(__nd); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 1974 | return __parent->__left_; | 
|  | 1975 | } | 
|  | 1976 | } | 
|  | 1977 | else if (value_comp()(__nd->__value_, __v)) | 
|  | 1978 | { | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 1979 | if (__nd->__right_ != nullptr) { | 
|  | 1980 | __nd_ptr = _VSTD::addressof(__nd->__right_); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 1981 | __nd = static_cast<__node_pointer>(__nd->__right_); | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 1982 | } else { | 
|  | 1983 | __parent = static_cast<__parent_pointer>(__nd); | 
|  | 1984 | return __nd->__right_; | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 1985 | } | 
|  | 1986 | } | 
|  | 1987 | else | 
|  | 1988 | { | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 1989 | __parent = static_cast<__parent_pointer>(__nd); | 
|  | 1990 | return *__nd_ptr; | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 1991 | } | 
|  | 1992 | } | 
|  | 1993 | } | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 1994 | __parent = static_cast<__parent_pointer>(__end_node()); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 1995 | return __parent->__left_; | 
|  | 1996 | } | 
|  | 1997 |  | 
|  | 1998 | // Find place to insert if __v doesn't exist | 
|  | 1999 | // First check prior to __hint. | 
|  | 2000 | // Next check after __hint. | 
|  | 2001 | // Next do O(log N) search. | 
|  | 2002 | // Set __parent to parent of null leaf | 
|  | 2003 | // Return reference to null leaf | 
|  | 2004 | // If __v exists, set parent to node of __v and return reference to node of __v | 
|  | 2005 | template <class _Tp, class _Compare, class _Allocator> | 
|  | 2006 | template <class _Key> | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 2007 | typename __tree<_Tp, _Compare, _Allocator>::__node_base_pointer& | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 2008 | __tree<_Tp, _Compare, _Allocator>::__find_equal(const_iterator __hint, | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 2009 | __parent_pointer& __parent, | 
|  | 2010 | __node_base_pointer& __dummy, | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 2011 | const _Key& __v) | 
|  | 2012 | { | 
|  | 2013 | if (__hint == end() || value_comp()(__v, *__hint)) // check before | 
|  | 2014 | { | 
|  | 2015 | // __v < *__hint | 
|  | 2016 | const_iterator __prior = __hint; | 
|  | 2017 | if (__prior == begin() || value_comp()(*--__prior, __v)) | 
|  | 2018 | { | 
|  | 2019 | // *prev(__hint) < __v < *__hint | 
|  | 2020 | if (__hint.__ptr_->__left_ == nullptr) | 
|  | 2021 | { | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 2022 | __parent = static_cast<__parent_pointer>(__hint.__ptr_); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 2023 | return __parent->__left_; | 
|  | 2024 | } | 
|  | 2025 | else | 
|  | 2026 | { | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 2027 | __parent = static_cast<__parent_pointer>(__prior.__ptr_); | 
|  | 2028 | return static_cast<__node_base_pointer>(__prior.__ptr_)->__right_; | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 2029 | } | 
|  | 2030 | } | 
|  | 2031 | // __v <= *prev(__hint) | 
|  | 2032 | return __find_equal(__parent, __v); | 
|  | 2033 | } | 
|  | 2034 | else if (value_comp()(*__hint, __v)) // check after | 
|  | 2035 | { | 
|  | 2036 | // *__hint < __v | 
| Howard Hinnant | 0949eed | 2011-06-30 21:18:19 | [diff] [blame] | 2037 | const_iterator __next = _VSTD::next(__hint); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 2038 | if (__next == end() || value_comp()(__v, *__next)) | 
|  | 2039 | { | 
| Howard Hinnant | 0949eed | 2011-06-30 21:18:19 | [diff] [blame] | 2040 | // *__hint < __v < *_VSTD::next(__hint) | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 2041 | if (__hint.__get_np()->__right_ == nullptr) | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 2042 | { | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 2043 | __parent = static_cast<__parent_pointer>(__hint.__ptr_); | 
|  | 2044 | return static_cast<__node_base_pointer>(__hint.__ptr_)->__right_; | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 2045 | } | 
|  | 2046 | else | 
|  | 2047 | { | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 2048 | __parent = static_cast<__parent_pointer>(__next.__ptr_); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 2049 | return __parent->__left_; | 
|  | 2050 | } | 
|  | 2051 | } | 
|  | 2052 | // *next(__hint) <= __v | 
|  | 2053 | return __find_equal(__parent, __v); | 
|  | 2054 | } | 
|  | 2055 | // else __v == *__hint | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 2056 | __parent = static_cast<__parent_pointer>(__hint.__ptr_); | 
|  | 2057 | __dummy = static_cast<__node_base_pointer>(__hint.__ptr_); | 
|  | 2058 | return __dummy; | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 2059 | } | 
|  | 2060 |  | 
|  | 2061 | template <class _Tp, class _Compare, class _Allocator> | 
|  | 2062 | void | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 2063 | __tree<_Tp, _Compare, _Allocator>::__insert_node_at(__parent_pointer __parent, | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 2064 | __node_base_pointer& __child, | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 2065 | __node_base_pointer __new_node) | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 2066 | { | 
|  | 2067 | __new_node->__left_ = nullptr; | 
|  | 2068 | __new_node->__right_ = nullptr; | 
|  | 2069 | __new_node->__parent_ = __parent; | 
| Eric Fiselier | db21506 | 2016-03-31 02:15:15 | [diff] [blame] | 2070 | // __new_node->__is_black_ is initialized in __tree_balance_after_insert | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 2071 | __child = __new_node; | 
|  | 2072 | if (__begin_node()->__left_ != nullptr) | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 2073 | __begin_node() = static_cast<__iter_pointer>(__begin_node()->__left_); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 2074 | __tree_balance_after_insert(__end_node()->__left_, __child); | 
|  | 2075 | ++size(); | 
|  | 2076 | } | 
|  | 2077 |  | 
| Eric Fiselier | db21506 | 2016-03-31 02:15:15 | [diff] [blame] | 2078 | #ifndef _LIBCPP_CXX03_LANG | 
|  | 2079 | template <class _Tp, class _Compare, class _Allocator> | 
|  | 2080 | template <class _Key, class... _Args> | 
|  | 2081 | pair<typename __tree<_Tp, _Compare, _Allocator>::iterator, bool> | 
|  | 2082 | __tree<_Tp, _Compare, _Allocator>::__emplace_unique_key_args(_Key const& __k, _Args&&... __args) | 
|  | 2083 | #else | 
|  | 2084 | template <class _Tp, class _Compare, class _Allocator> | 
|  | 2085 | template <class _Key, class _Args> | 
|  | 2086 | pair<typename __tree<_Tp, _Compare, _Allocator>::iterator, bool> | 
|  | 2087 | __tree<_Tp, _Compare, _Allocator>::__emplace_unique_key_args(_Key const& __k, _Args& __args) | 
|  | 2088 | #endif | 
|  | 2089 | { | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 2090 | __parent_pointer __parent; | 
| Eric Fiselier | db21506 | 2016-03-31 02:15:15 | [diff] [blame] | 2091 | __node_base_pointer& __child = __find_equal(__parent, __k); | 
|  | 2092 | __node_pointer __r = static_cast<__node_pointer>(__child); | 
|  | 2093 | bool __inserted = false; | 
|  | 2094 | if (__child == nullptr) | 
|  | 2095 | { | 
|  | 2096 | #ifndef _LIBCPP_CXX03_LANG | 
|  | 2097 | __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...); | 
|  | 2098 | #else | 
|  | 2099 | __node_holder __h = __construct_node(__args); | 
|  | 2100 | #endif | 
|  | 2101 | __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get())); | 
|  | 2102 | __r = __h.release(); | 
|  | 2103 | __inserted = true; | 
|  | 2104 | } | 
|  | 2105 | return pair<iterator, bool>(iterator(__r), __inserted); | 
|  | 2106 | } | 
|  | 2107 |  | 
|  | 2108 |  | 
|  | 2109 | #ifndef _LIBCPP_CXX03_LANG | 
|  | 2110 | template <class _Tp, class _Compare, class _Allocator> | 
|  | 2111 | template <class _Key, class... _Args> | 
|  | 2112 | typename __tree<_Tp, _Compare, _Allocator>::iterator | 
|  | 2113 | __tree<_Tp, _Compare, _Allocator>::__emplace_hint_unique_key_args( | 
|  | 2114 | const_iterator __p, _Key const& __k, _Args&&... __args) | 
|  | 2115 | #else | 
|  | 2116 | template <class _Tp, class _Compare, class _Allocator> | 
|  | 2117 | template <class _Key, class _Args> | 
|  | 2118 | typename __tree<_Tp, _Compare, _Allocator>::iterator | 
|  | 2119 | __tree<_Tp, _Compare, _Allocator>::__emplace_hint_unique_key_args( | 
|  | 2120 | const_iterator __p, _Key const& __k, _Args& __args) | 
|  | 2121 | #endif | 
|  | 2122 | { | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 2123 | __parent_pointer __parent; | 
|  | 2124 | __node_base_pointer __dummy; | 
|  | 2125 | __node_base_pointer& __child = __find_equal(__p, __parent, __dummy, __k); | 
| Eric Fiselier | db21506 | 2016-03-31 02:15:15 | [diff] [blame] | 2126 | __node_pointer __r = static_cast<__node_pointer>(__child); | 
|  | 2127 | if (__child == nullptr) | 
|  | 2128 | { | 
|  | 2129 | #ifndef _LIBCPP_CXX03_LANG | 
|  | 2130 | __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...); | 
|  | 2131 | #else | 
|  | 2132 | __node_holder __h = __construct_node(__args); | 
|  | 2133 | #endif | 
|  | 2134 | __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get())); | 
|  | 2135 | __r = __h.release(); | 
|  | 2136 | } | 
|  | 2137 | return iterator(__r); | 
|  | 2138 | } | 
|  | 2139 |  | 
|  | 2140 |  | 
|  | 2141 | #ifndef _LIBCPP_CXX03_LANG | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 2142 |  | 
|  | 2143 | template <class _Tp, class _Compare, class _Allocator> | 
|  | 2144 | template <class ..._Args> | 
|  | 2145 | typename __tree<_Tp, _Compare, _Allocator>::__node_holder | 
|  | 2146 | __tree<_Tp, _Compare, _Allocator>::__construct_node(_Args&& ...__args) | 
|  | 2147 | { | 
| Eric Fiselier | db21506 | 2016-03-31 02:15:15 | [diff] [blame] | 2148 | static_assert(!__is_tree_value_type<_Args...>::value, | 
|  | 2149 | "Cannot construct from __value_type"); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 2150 | __node_allocator& __na = __node_alloc(); | 
| Howard Hinnant | 9996844 | 2011-11-29 18:15:50 | [diff] [blame] | 2151 | __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na)); | 
| Eric Fiselier | db21506 | 2016-03-31 02:15:15 | [diff] [blame] | 2152 | __node_traits::construct(__na, _NodeTypes::__get_ptr(__h->__value_), _VSTD::forward<_Args>(__args)...); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 2153 | __h.get_deleter().__value_constructed = true; | 
|  | 2154 | return __h; | 
|  | 2155 | } | 
|  | 2156 |  | 
| Eric Fiselier | db21506 | 2016-03-31 02:15:15 | [diff] [blame] | 2157 |  | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 2158 | template <class _Tp, class _Compare, class _Allocator> | 
|  | 2159 | template <class... _Args> | 
|  | 2160 | pair<typename __tree<_Tp, _Compare, _Allocator>::iterator, bool> | 
| Eric Fiselier | 83c9dc1 | 2016-04-15 23:27:27 | [diff] [blame] | 2161 | __tree<_Tp, _Compare, _Allocator>::__emplace_unique_impl(_Args&&... __args) | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 2162 | { | 
| Howard Hinnant | 0949eed | 2011-06-30 21:18:19 | [diff] [blame] | 2163 | __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...); | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 2164 | __parent_pointer __parent; | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 2165 | __node_base_pointer& __child = __find_equal(__parent, __h->__value_); | 
|  | 2166 | __node_pointer __r = static_cast<__node_pointer>(__child); | 
|  | 2167 | bool __inserted = false; | 
|  | 2168 | if (__child == nullptr) | 
|  | 2169 | { | 
| Howard Hinnant | 70342b9 | 2013-06-19 21:29:40 | [diff] [blame] | 2170 | __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get())); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 2171 | __r = __h.release(); | 
|  | 2172 | __inserted = true; | 
|  | 2173 | } | 
|  | 2174 | return pair<iterator, bool>(iterator(__r), __inserted); | 
|  | 2175 | } | 
|  | 2176 |  | 
|  | 2177 | template <class _Tp, class _Compare, class _Allocator> | 
|  | 2178 | template <class... _Args> | 
|  | 2179 | typename __tree<_Tp, _Compare, _Allocator>::iterator | 
| Eric Fiselier | 83c9dc1 | 2016-04-15 23:27:27 | [diff] [blame] | 2180 | __tree<_Tp, _Compare, _Allocator>::__emplace_hint_unique_impl(const_iterator __p, _Args&&... __args) | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 2181 | { | 
| Howard Hinnant | 0949eed | 2011-06-30 21:18:19 | [diff] [blame] | 2182 | __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...); | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 2183 | __parent_pointer __parent; | 
|  | 2184 | __node_base_pointer __dummy; | 
|  | 2185 | __node_base_pointer& __child = __find_equal(__p, __parent, __dummy, __h->__value_); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 2186 | __node_pointer __r = static_cast<__node_pointer>(__child); | 
|  | 2187 | if (__child == nullptr) | 
|  | 2188 | { | 
| Howard Hinnant | 70342b9 | 2013-06-19 21:29:40 | [diff] [blame] | 2189 | __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get())); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 2190 | __r = __h.release(); | 
|  | 2191 | } | 
|  | 2192 | return iterator(__r); | 
|  | 2193 | } | 
|  | 2194 |  | 
|  | 2195 | template <class _Tp, class _Compare, class _Allocator> | 
|  | 2196 | template <class... _Args> | 
|  | 2197 | typename __tree<_Tp, _Compare, _Allocator>::iterator | 
|  | 2198 | __tree<_Tp, _Compare, _Allocator>::__emplace_multi(_Args&&... __args) | 
|  | 2199 | { | 
| Howard Hinnant | 0949eed | 2011-06-30 21:18:19 | [diff] [blame] | 2200 | __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...); | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 2201 | __parent_pointer __parent; | 
| Eric Fiselier | db21506 | 2016-03-31 02:15:15 | [diff] [blame] | 2202 | __node_base_pointer& __child = __find_leaf_high(__parent, _NodeTypes::__get_key(__h->__value_)); | 
| Howard Hinnant | 70342b9 | 2013-06-19 21:29:40 | [diff] [blame] | 2203 | __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get())); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 2204 | return iterator(static_cast<__node_pointer>(__h.release())); | 
|  | 2205 | } | 
|  | 2206 |  | 
|  | 2207 | template <class _Tp, class _Compare, class _Allocator> | 
|  | 2208 | template <class... _Args> | 
|  | 2209 | typename __tree<_Tp, _Compare, _Allocator>::iterator | 
|  | 2210 | __tree<_Tp, _Compare, _Allocator>::__emplace_hint_multi(const_iterator __p, | 
|  | 2211 | _Args&&... __args) | 
|  | 2212 | { | 
| Howard Hinnant | 0949eed | 2011-06-30 21:18:19 | [diff] [blame] | 2213 | __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...); | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 2214 | __parent_pointer __parent; | 
| Eric Fiselier | db21506 | 2016-03-31 02:15:15 | [diff] [blame] | 2215 | __node_base_pointer& __child = __find_leaf(__p, __parent, _NodeTypes::__get_key(__h->__value_)); | 
| Howard Hinnant | 70342b9 | 2013-06-19 21:29:40 | [diff] [blame] | 2216 | __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get())); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 2217 | return iterator(static_cast<__node_pointer>(__h.release())); | 
|  | 2218 | } | 
|  | 2219 |  | 
| Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 | [diff] [blame] | 2220 |  | 
| Eric Fiselier | db21506 | 2016-03-31 02:15:15 | [diff] [blame] | 2221 | #else // _LIBCPP_CXX03_LANG | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 2222 |  | 
|  | 2223 | template <class _Tp, class _Compare, class _Allocator> | 
|  | 2224 | typename __tree<_Tp, _Compare, _Allocator>::__node_holder | 
| Eric Fiselier | db21506 | 2016-03-31 02:15:15 | [diff] [blame] | 2225 | __tree<_Tp, _Compare, _Allocator>::__construct_node(const __container_value_type& __v) | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 2226 | { | 
|  | 2227 | __node_allocator& __na = __node_alloc(); | 
| Howard Hinnant | 9996844 | 2011-11-29 18:15:50 | [diff] [blame] | 2228 | __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na)); | 
| Eric Fiselier | db21506 | 2016-03-31 02:15:15 | [diff] [blame] | 2229 | __node_traits::construct(__na, _NodeTypes::__get_ptr(__h->__value_), __v); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 2230 | __h.get_deleter().__value_constructed = true; | 
| Dimitry Andric | 8966350 | 2015-08-19 06:43:33 | [diff] [blame] | 2231 | return _LIBCPP_EXPLICIT_MOVE(__h); // explicitly moved for C++03 | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 2232 | } | 
|  | 2233 |  | 
| Eric Fiselier | db21506 | 2016-03-31 02:15:15 | [diff] [blame] | 2234 | #endif // _LIBCPP_CXX03_LANG | 
| Howard Hinnant | d0a2fbf | 2011-03-10 17:27:57 | [diff] [blame] | 2235 |  | 
| Eric Fiselier | db21506 | 2016-03-31 02:15:15 | [diff] [blame] | 2236 | #ifdef _LIBCPP_CXX03_LANG | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 2237 | template <class _Tp, class _Compare, class _Allocator> | 
|  | 2238 | typename __tree<_Tp, _Compare, _Allocator>::iterator | 
| Eric Fiselier | db21506 | 2016-03-31 02:15:15 | [diff] [blame] | 2239 | __tree<_Tp, _Compare, _Allocator>::__insert_multi(const __container_value_type& __v) | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 2240 | { | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 2241 | __parent_pointer __parent; | 
| Eric Fiselier | db21506 | 2016-03-31 02:15:15 | [diff] [blame] | 2242 | __node_base_pointer& __child = __find_leaf_high(__parent, _NodeTypes::__get_key(__v)); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 2243 | __node_holder __h = __construct_node(__v); | 
| Howard Hinnant | 70342b9 | 2013-06-19 21:29:40 | [diff] [blame] | 2244 | __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get())); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 2245 | return iterator(__h.release()); | 
|  | 2246 | } | 
|  | 2247 |  | 
|  | 2248 | template <class _Tp, class _Compare, class _Allocator> | 
|  | 2249 | typename __tree<_Tp, _Compare, _Allocator>::iterator | 
| Eric Fiselier | db21506 | 2016-03-31 02:15:15 | [diff] [blame] | 2250 | __tree<_Tp, _Compare, _Allocator>::__insert_multi(const_iterator __p, const __container_value_type& __v) | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 2251 | { | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 2252 | __parent_pointer __parent; | 
| Eric Fiselier | db21506 | 2016-03-31 02:15:15 | [diff] [blame] | 2253 | __node_base_pointer& __child = __find_leaf(__p, __parent, _NodeTypes::__get_key(__v)); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 2254 | __node_holder __h = __construct_node(__v); | 
| Howard Hinnant | 70342b9 | 2013-06-19 21:29:40 | [diff] [blame] | 2255 | __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get())); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 2256 | return iterator(__h.release()); | 
|  | 2257 | } | 
| Eric Fiselier | db21506 | 2016-03-31 02:15:15 | [diff] [blame] | 2258 | #endif | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 2259 |  | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 2260 | template <class _Tp, class _Compare, class _Allocator> | 
|  | 2261 | pair<typename __tree<_Tp, _Compare, _Allocator>::iterator, bool> | 
|  | 2262 | __tree<_Tp, _Compare, _Allocator>::__node_insert_unique(__node_pointer __nd) | 
|  | 2263 | { | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 2264 | __parent_pointer __parent; | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 2265 | __node_base_pointer& __child = __find_equal(__parent, __nd->__value_); | 
|  | 2266 | __node_pointer __r = static_cast<__node_pointer>(__child); | 
|  | 2267 | bool __inserted = false; | 
|  | 2268 | if (__child == nullptr) | 
|  | 2269 | { | 
| Howard Hinnant | 70342b9 | 2013-06-19 21:29:40 | [diff] [blame] | 2270 | __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__nd)); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 2271 | __r = __nd; | 
|  | 2272 | __inserted = true; | 
|  | 2273 | } | 
|  | 2274 | return pair<iterator, bool>(iterator(__r), __inserted); | 
|  | 2275 | } | 
|  | 2276 |  | 
|  | 2277 | template <class _Tp, class _Compare, class _Allocator> | 
|  | 2278 | typename __tree<_Tp, _Compare, _Allocator>::iterator | 
|  | 2279 | __tree<_Tp, _Compare, _Allocator>::__node_insert_unique(const_iterator __p, | 
|  | 2280 | __node_pointer __nd) | 
|  | 2281 | { | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 2282 | __parent_pointer __parent; | 
|  | 2283 | __node_base_pointer __dummy; | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 2284 | __node_base_pointer& __child = __find_equal(__p, __parent, __nd->__value_); | 
|  | 2285 | __node_pointer __r = static_cast<__node_pointer>(__child); | 
|  | 2286 | if (__child == nullptr) | 
|  | 2287 | { | 
| Howard Hinnant | 70342b9 | 2013-06-19 21:29:40 | [diff] [blame] | 2288 | __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__nd)); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 2289 | __r = __nd; | 
|  | 2290 | } | 
|  | 2291 | return iterator(__r); | 
|  | 2292 | } | 
|  | 2293 |  | 
|  | 2294 | template <class _Tp, class _Compare, class _Allocator> | 
|  | 2295 | typename __tree<_Tp, _Compare, _Allocator>::iterator | 
|  | 2296 | __tree<_Tp, _Compare, _Allocator>::__node_insert_multi(__node_pointer __nd) | 
|  | 2297 | { | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 2298 | __parent_pointer __parent; | 
| Eric Fiselier | db21506 | 2016-03-31 02:15:15 | [diff] [blame] | 2299 | __node_base_pointer& __child = __find_leaf_high(__parent, _NodeTypes::__get_key(__nd->__value_)); | 
| Howard Hinnant | 70342b9 | 2013-06-19 21:29:40 | [diff] [blame] | 2300 | __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__nd)); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 2301 | return iterator(__nd); | 
|  | 2302 | } | 
|  | 2303 |  | 
|  | 2304 | template <class _Tp, class _Compare, class _Allocator> | 
|  | 2305 | typename __tree<_Tp, _Compare, _Allocator>::iterator | 
|  | 2306 | __tree<_Tp, _Compare, _Allocator>::__node_insert_multi(const_iterator __p, | 
|  | 2307 | __node_pointer __nd) | 
|  | 2308 | { | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 2309 | __parent_pointer __parent; | 
| Eric Fiselier | db21506 | 2016-03-31 02:15:15 | [diff] [blame] | 2310 | __node_base_pointer& __child = __find_leaf(__p, __parent, _NodeTypes::__get_key(__nd->__value_)); | 
| Howard Hinnant | 70342b9 | 2013-06-19 21:29:40 | [diff] [blame] | 2311 | __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__nd)); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 2312 | return iterator(__nd); | 
|  | 2313 | } | 
|  | 2314 |  | 
|  | 2315 | template <class _Tp, class _Compare, class _Allocator> | 
|  | 2316 | typename __tree<_Tp, _Compare, _Allocator>::iterator | 
|  | 2317 | __tree<_Tp, _Compare, _Allocator>::erase(const_iterator __p) | 
|  | 2318 | { | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 2319 | __node_pointer __np = __p.__get_np(); | 
|  | 2320 | iterator __r(__p.__ptr_); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 2321 | ++__r; | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 2322 | if (__begin_node() == __p.__ptr_) | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 2323 | __begin_node() = __r.__ptr_; | 
|  | 2324 | --size(); | 
|  | 2325 | __node_allocator& __na = __node_alloc(); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 2326 | __tree_remove(__end_node()->__left_, | 
|  | 2327 | static_cast<__node_base_pointer>(__np)); | 
| Eric Fiselier | db21506 | 2016-03-31 02:15:15 | [diff] [blame] | 2328 | __node_traits::destroy(__na, _NodeTypes::__get_ptr( | 
|  | 2329 | const_cast<__node_value_type&>(*__p))); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 2330 | __node_traits::deallocate(__na, __np, 1); | 
|  | 2331 | return __r; | 
|  | 2332 | } | 
|  | 2333 |  | 
|  | 2334 | template <class _Tp, class _Compare, class _Allocator> | 
|  | 2335 | typename __tree<_Tp, _Compare, _Allocator>::iterator | 
|  | 2336 | __tree<_Tp, _Compare, _Allocator>::erase(const_iterator __f, const_iterator __l) | 
|  | 2337 | { | 
|  | 2338 | while (__f != __l) | 
|  | 2339 | __f = erase(__f); | 
| Howard Hinnant | 70342b9 | 2013-06-19 21:29:40 | [diff] [blame] | 2340 | return iterator(__l.__ptr_); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 2341 | } | 
|  | 2342 |  | 
|  | 2343 | template <class _Tp, class _Compare, class _Allocator> | 
|  | 2344 | template <class _Key> | 
|  | 2345 | typename __tree<_Tp, _Compare, _Allocator>::size_type | 
|  | 2346 | __tree<_Tp, _Compare, _Allocator>::__erase_unique(const _Key& __k) | 
|  | 2347 | { | 
|  | 2348 | iterator __i = find(__k); | 
|  | 2349 | if (__i == end()) | 
|  | 2350 | return 0; | 
|  | 2351 | erase(__i); | 
|  | 2352 | return 1; | 
|  | 2353 | } | 
|  | 2354 |  | 
|  | 2355 | template <class _Tp, class _Compare, class _Allocator> | 
|  | 2356 | template <class _Key> | 
|  | 2357 | typename __tree<_Tp, _Compare, _Allocator>::size_type | 
|  | 2358 | __tree<_Tp, _Compare, _Allocator>::__erase_multi(const _Key& __k) | 
|  | 2359 | { | 
|  | 2360 | pair<iterator, iterator> __p = __equal_range_multi(__k); | 
|  | 2361 | size_type __r = 0; | 
|  | 2362 | for (; __p.first != __p.second; ++__r) | 
|  | 2363 | __p.first = erase(__p.first); | 
|  | 2364 | return __r; | 
|  | 2365 | } | 
|  | 2366 |  | 
|  | 2367 | template <class _Tp, class _Compare, class _Allocator> | 
|  | 2368 | template <class _Key> | 
|  | 2369 | typename __tree<_Tp, _Compare, _Allocator>::iterator | 
|  | 2370 | __tree<_Tp, _Compare, _Allocator>::find(const _Key& __v) | 
|  | 2371 | { | 
|  | 2372 | iterator __p = __lower_bound(__v, __root(), __end_node()); | 
|  | 2373 | if (__p != end() && !value_comp()(__v, *__p)) | 
|  | 2374 | return __p; | 
|  | 2375 | return end(); | 
|  | 2376 | } | 
|  | 2377 |  | 
|  | 2378 | template <class _Tp, class _Compare, class _Allocator> | 
|  | 2379 | template <class _Key> | 
|  | 2380 | typename __tree<_Tp, _Compare, _Allocator>::const_iterator | 
|  | 2381 | __tree<_Tp, _Compare, _Allocator>::find(const _Key& __v) const | 
|  | 2382 | { | 
|  | 2383 | const_iterator __p = __lower_bound(__v, __root(), __end_node()); | 
|  | 2384 | if (__p != end() && !value_comp()(__v, *__p)) | 
|  | 2385 | return __p; | 
|  | 2386 | return end(); | 
|  | 2387 | } | 
|  | 2388 |  | 
|  | 2389 | template <class _Tp, class _Compare, class _Allocator> | 
|  | 2390 | template <class _Key> | 
|  | 2391 | typename __tree<_Tp, _Compare, _Allocator>::size_type | 
|  | 2392 | __tree<_Tp, _Compare, _Allocator>::__count_unique(const _Key& __k) const | 
|  | 2393 | { | 
| Eric Fiselier | 227b47c | 2016-02-20 07:12:17 | [diff] [blame] | 2394 | __node_pointer __rt = __root(); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 2395 | while (__rt != nullptr) | 
|  | 2396 | { | 
|  | 2397 | if (value_comp()(__k, __rt->__value_)) | 
|  | 2398 | { | 
| Eric Fiselier | 227b47c | 2016-02-20 07:12:17 | [diff] [blame] | 2399 | __rt = static_cast<__node_pointer>(__rt->__left_); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 2400 | } | 
|  | 2401 | else if (value_comp()(__rt->__value_, __k)) | 
| Eric Fiselier | 227b47c | 2016-02-20 07:12:17 | [diff] [blame] | 2402 | __rt = static_cast<__node_pointer>(__rt->__right_); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 2403 | else | 
|  | 2404 | return 1; | 
|  | 2405 | } | 
|  | 2406 | return 0; | 
|  | 2407 | } | 
|  | 2408 |  | 
|  | 2409 | template <class _Tp, class _Compare, class _Allocator> | 
|  | 2410 | template <class _Key> | 
|  | 2411 | typename __tree<_Tp, _Compare, _Allocator>::size_type | 
|  | 2412 | __tree<_Tp, _Compare, _Allocator>::__count_multi(const _Key& __k) const | 
|  | 2413 | { | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 2414 | __iter_pointer __result = __end_node(); | 
| Eric Fiselier | 227b47c | 2016-02-20 07:12:17 | [diff] [blame] | 2415 | __node_pointer __rt = __root(); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 2416 | while (__rt != nullptr) | 
|  | 2417 | { | 
|  | 2418 | if (value_comp()(__k, __rt->__value_)) | 
|  | 2419 | { | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 2420 | __result = static_cast<__iter_pointer>(__rt); | 
| Eric Fiselier | 227b47c | 2016-02-20 07:12:17 | [diff] [blame] | 2421 | __rt = static_cast<__node_pointer>(__rt->__left_); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 2422 | } | 
|  | 2423 | else if (value_comp()(__rt->__value_, __k)) | 
| Eric Fiselier | 227b47c | 2016-02-20 07:12:17 | [diff] [blame] | 2424 | __rt = static_cast<__node_pointer>(__rt->__right_); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 2425 | else | 
| Howard Hinnant | 0949eed | 2011-06-30 21:18:19 | [diff] [blame] | 2426 | return _VSTD::distance( | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 2427 | __lower_bound(__k, static_cast<__node_pointer>(__rt->__left_), static_cast<__iter_pointer>(__rt)), | 
| Eric Fiselier | 227b47c | 2016-02-20 07:12:17 | [diff] [blame] | 2428 | __upper_bound(__k, static_cast<__node_pointer>(__rt->__right_), __result) | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 2429 | ); | 
|  | 2430 | } | 
|  | 2431 | return 0; | 
|  | 2432 | } | 
|  | 2433 |  | 
|  | 2434 | template <class _Tp, class _Compare, class _Allocator> | 
|  | 2435 | template <class _Key> | 
|  | 2436 | typename __tree<_Tp, _Compare, _Allocator>::iterator | 
|  | 2437 | __tree<_Tp, _Compare, _Allocator>::__lower_bound(const _Key& __v, | 
|  | 2438 | __node_pointer __root, | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 2439 | __iter_pointer __result) | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 2440 | { | 
|  | 2441 | while (__root != nullptr) | 
|  | 2442 | { | 
|  | 2443 | if (!value_comp()(__root->__value_, __v)) | 
|  | 2444 | { | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 2445 | __result = static_cast<__iter_pointer>(__root); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 2446 | __root = static_cast<__node_pointer>(__root->__left_); | 
|  | 2447 | } | 
|  | 2448 | else | 
|  | 2449 | __root = static_cast<__node_pointer>(__root->__right_); | 
|  | 2450 | } | 
|  | 2451 | return iterator(__result); | 
|  | 2452 | } | 
|  | 2453 |  | 
|  | 2454 | template <class _Tp, class _Compare, class _Allocator> | 
|  | 2455 | template <class _Key> | 
|  | 2456 | typename __tree<_Tp, _Compare, _Allocator>::const_iterator | 
|  | 2457 | __tree<_Tp, _Compare, _Allocator>::__lower_bound(const _Key& __v, | 
| Eric Fiselier | 227b47c | 2016-02-20 07:12:17 | [diff] [blame] | 2458 | __node_pointer __root, | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 2459 | __iter_pointer __result) const | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 2460 | { | 
|  | 2461 | while (__root != nullptr) | 
|  | 2462 | { | 
|  | 2463 | if (!value_comp()(__root->__value_, __v)) | 
|  | 2464 | { | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 2465 | __result = static_cast<__iter_pointer>(__root); | 
| Eric Fiselier | 227b47c | 2016-02-20 07:12:17 | [diff] [blame] | 2466 | __root = static_cast<__node_pointer>(__root->__left_); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 2467 | } | 
|  | 2468 | else | 
| Eric Fiselier | 227b47c | 2016-02-20 07:12:17 | [diff] [blame] | 2469 | __root = static_cast<__node_pointer>(__root->__right_); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 2470 | } | 
|  | 2471 | return const_iterator(__result); | 
|  | 2472 | } | 
|  | 2473 |  | 
|  | 2474 | template <class _Tp, class _Compare, class _Allocator> | 
|  | 2475 | template <class _Key> | 
|  | 2476 | typename __tree<_Tp, _Compare, _Allocator>::iterator | 
|  | 2477 | __tree<_Tp, _Compare, _Allocator>::__upper_bound(const _Key& __v, | 
|  | 2478 | __node_pointer __root, | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 2479 | __iter_pointer __result) | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 2480 | { | 
|  | 2481 | while (__root != nullptr) | 
|  | 2482 | { | 
|  | 2483 | if (value_comp()(__v, __root->__value_)) | 
|  | 2484 | { | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 2485 | __result = static_cast<__iter_pointer>(__root); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 2486 | __root = static_cast<__node_pointer>(__root->__left_); | 
|  | 2487 | } | 
|  | 2488 | else | 
|  | 2489 | __root = static_cast<__node_pointer>(__root->__right_); | 
|  | 2490 | } | 
|  | 2491 | return iterator(__result); | 
|  | 2492 | } | 
|  | 2493 |  | 
|  | 2494 | template <class _Tp, class _Compare, class _Allocator> | 
|  | 2495 | template <class _Key> | 
|  | 2496 | typename __tree<_Tp, _Compare, _Allocator>::const_iterator | 
|  | 2497 | __tree<_Tp, _Compare, _Allocator>::__upper_bound(const _Key& __v, | 
| Eric Fiselier | 227b47c | 2016-02-20 07:12:17 | [diff] [blame] | 2498 | __node_pointer __root, | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 2499 | __iter_pointer __result) const | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 2500 | { | 
|  | 2501 | while (__root != nullptr) | 
|  | 2502 | { | 
|  | 2503 | if (value_comp()(__v, __root->__value_)) | 
|  | 2504 | { | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 2505 | __result = static_cast<__iter_pointer>(__root); | 
| Eric Fiselier | 227b47c | 2016-02-20 07:12:17 | [diff] [blame] | 2506 | __root = static_cast<__node_pointer>(__root->__left_); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 2507 | } | 
|  | 2508 | else | 
| Eric Fiselier | 227b47c | 2016-02-20 07:12:17 | [diff] [blame] | 2509 | __root = static_cast<__node_pointer>(__root->__right_); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 2510 | } | 
|  | 2511 | return const_iterator(__result); | 
|  | 2512 | } | 
|  | 2513 |  | 
|  | 2514 | template <class _Tp, class _Compare, class _Allocator> | 
|  | 2515 | template <class _Key> | 
|  | 2516 | pair<typename __tree<_Tp, _Compare, _Allocator>::iterator, | 
|  | 2517 | typename __tree<_Tp, _Compare, _Allocator>::iterator> | 
|  | 2518 | __tree<_Tp, _Compare, _Allocator>::__equal_range_unique(const _Key& __k) | 
|  | 2519 | { | 
| Howard Hinnant | 9996844 | 2011-11-29 18:15:50 | [diff] [blame] | 2520 | typedef pair<iterator, iterator> _Pp; | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 2521 | __iter_pointer __result = __end_node(); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 2522 | __node_pointer __rt = __root(); | 
|  | 2523 | while (__rt != nullptr) | 
|  | 2524 | { | 
|  | 2525 | if (value_comp()(__k, __rt->__value_)) | 
|  | 2526 | { | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 2527 | __result = static_cast<__iter_pointer>(__rt); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 2528 | __rt = static_cast<__node_pointer>(__rt->__left_); | 
|  | 2529 | } | 
|  | 2530 | else if (value_comp()(__rt->__value_, __k)) | 
|  | 2531 | __rt = static_cast<__node_pointer>(__rt->__right_); | 
|  | 2532 | else | 
| Howard Hinnant | 9996844 | 2011-11-29 18:15:50 | [diff] [blame] | 2533 | return _Pp(iterator(__rt), | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 2534 | iterator( | 
|  | 2535 | __rt->__right_ != nullptr ? | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 2536 | static_cast<__iter_pointer>(__tree_min(__rt->__right_)) | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 2537 | : __result)); | 
|  | 2538 | } | 
| Howard Hinnant | 9996844 | 2011-11-29 18:15:50 | [diff] [blame] | 2539 | return _Pp(iterator(__result), iterator(__result)); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 2540 | } | 
|  | 2541 |  | 
|  | 2542 | template <class _Tp, class _Compare, class _Allocator> | 
|  | 2543 | template <class _Key> | 
|  | 2544 | pair<typename __tree<_Tp, _Compare, _Allocator>::const_iterator, | 
|  | 2545 | typename __tree<_Tp, _Compare, _Allocator>::const_iterator> | 
|  | 2546 | __tree<_Tp, _Compare, _Allocator>::__equal_range_unique(const _Key& __k) const | 
|  | 2547 | { | 
| Howard Hinnant | 9996844 | 2011-11-29 18:15:50 | [diff] [blame] | 2548 | typedef pair<const_iterator, const_iterator> _Pp; | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 2549 | __iter_pointer __result = __end_node(); | 
| Eric Fiselier | 227b47c | 2016-02-20 07:12:17 | [diff] [blame] | 2550 | __node_pointer __rt = __root(); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 2551 | while (__rt != nullptr) | 
|  | 2552 | { | 
|  | 2553 | if (value_comp()(__k, __rt->__value_)) | 
|  | 2554 | { | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 2555 | __result = static_cast<__iter_pointer>(__rt); | 
| Eric Fiselier | 227b47c | 2016-02-20 07:12:17 | [diff] [blame] | 2556 | __rt = static_cast<__node_pointer>(__rt->__left_); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 2557 | } | 
|  | 2558 | else if (value_comp()(__rt->__value_, __k)) | 
| Eric Fiselier | 227b47c | 2016-02-20 07:12:17 | [diff] [blame] | 2559 | __rt = static_cast<__node_pointer>(__rt->__right_); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 2560 | else | 
| Howard Hinnant | 9996844 | 2011-11-29 18:15:50 | [diff] [blame] | 2561 | return _Pp(const_iterator(__rt), | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 2562 | const_iterator( | 
|  | 2563 | __rt->__right_ != nullptr ? | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 2564 | static_cast<__iter_pointer>(__tree_min(__rt->__right_)) | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 2565 | : __result)); | 
|  | 2566 | } | 
| Howard Hinnant | 9996844 | 2011-11-29 18:15:50 | [diff] [blame] | 2567 | return _Pp(const_iterator(__result), const_iterator(__result)); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 2568 | } | 
|  | 2569 |  | 
|  | 2570 | template <class _Tp, class _Compare, class _Allocator> | 
|  | 2571 | template <class _Key> | 
|  | 2572 | pair<typename __tree<_Tp, _Compare, _Allocator>::iterator, | 
|  | 2573 | typename __tree<_Tp, _Compare, _Allocator>::iterator> | 
|  | 2574 | __tree<_Tp, _Compare, _Allocator>::__equal_range_multi(const _Key& __k) | 
|  | 2575 | { | 
| Howard Hinnant | 9996844 | 2011-11-29 18:15:50 | [diff] [blame] | 2576 | typedef pair<iterator, iterator> _Pp; | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 2577 | __iter_pointer __result = __end_node(); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 2578 | __node_pointer __rt = __root(); | 
|  | 2579 | while (__rt != nullptr) | 
|  | 2580 | { | 
|  | 2581 | if (value_comp()(__k, __rt->__value_)) | 
|  | 2582 | { | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 2583 | __result = static_cast<__iter_pointer>(__rt); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 2584 | __rt = static_cast<__node_pointer>(__rt->__left_); | 
|  | 2585 | } | 
|  | 2586 | else if (value_comp()(__rt->__value_, __k)) | 
|  | 2587 | __rt = static_cast<__node_pointer>(__rt->__right_); | 
|  | 2588 | else | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 2589 | return _Pp(__lower_bound(__k, static_cast<__node_pointer>(__rt->__left_), static_cast<__iter_pointer>(__rt)), | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 2590 | __upper_bound(__k, static_cast<__node_pointer>(__rt->__right_), __result)); | 
|  | 2591 | } | 
| Howard Hinnant | 9996844 | 2011-11-29 18:15:50 | [diff] [blame] | 2592 | return _Pp(iterator(__result), iterator(__result)); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 2593 | } | 
|  | 2594 |  | 
|  | 2595 | template <class _Tp, class _Compare, class _Allocator> | 
|  | 2596 | template <class _Key> | 
|  | 2597 | pair<typename __tree<_Tp, _Compare, _Allocator>::const_iterator, | 
|  | 2598 | typename __tree<_Tp, _Compare, _Allocator>::const_iterator> | 
|  | 2599 | __tree<_Tp, _Compare, _Allocator>::__equal_range_multi(const _Key& __k) const | 
|  | 2600 | { | 
| Howard Hinnant | 9996844 | 2011-11-29 18:15:50 | [diff] [blame] | 2601 | typedef pair<const_iterator, const_iterator> _Pp; | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 2602 | __iter_pointer __result = __end_node(); | 
| Eric Fiselier | 227b47c | 2016-02-20 07:12:17 | [diff] [blame] | 2603 | __node_pointer __rt = __root(); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 2604 | while (__rt != nullptr) | 
|  | 2605 | { | 
|  | 2606 | if (value_comp()(__k, __rt->__value_)) | 
|  | 2607 | { | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 2608 | __result = static_cast<__iter_pointer>(__rt); | 
| Eric Fiselier | 227b47c | 2016-02-20 07:12:17 | [diff] [blame] | 2609 | __rt = static_cast<__node_pointer>(__rt->__left_); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 2610 | } | 
|  | 2611 | else if (value_comp()(__rt->__value_, __k)) | 
| Eric Fiselier | 227b47c | 2016-02-20 07:12:17 | [diff] [blame] | 2612 | __rt = static_cast<__node_pointer>(__rt->__right_); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 2613 | else | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 2614 | return _Pp(__lower_bound(__k, static_cast<__node_pointer>(__rt->__left_), static_cast<__iter_pointer>(__rt)), | 
| Eric Fiselier | 227b47c | 2016-02-20 07:12:17 | [diff] [blame] | 2615 | __upper_bound(__k, static_cast<__node_pointer>(__rt->__right_), __result)); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 2616 | } | 
| Howard Hinnant | 9996844 | 2011-11-29 18:15:50 | [diff] [blame] | 2617 | return _Pp(const_iterator(__result), const_iterator(__result)); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 2618 | } | 
|  | 2619 |  | 
|  | 2620 | template <class _Tp, class _Compare, class _Allocator> | 
|  | 2621 | typename __tree<_Tp, _Compare, _Allocator>::__node_holder | 
| Howard Hinnant | 8b53768 | 2011-06-04 17:10:24 | [diff] [blame] | 2622 | __tree<_Tp, _Compare, _Allocator>::remove(const_iterator __p) _NOEXCEPT | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 2623 | { | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 2624 | __node_pointer __np = __p.__get_np(); | 
|  | 2625 | if (__begin_node() == __p.__ptr_) | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 2626 | { | 
|  | 2627 | if (__np->__right_ != nullptr) | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 2628 | __begin_node() = static_cast<__iter_pointer>(__np->__right_); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 2629 | else | 
| Eric Fiselier | 7310ec8 | 2016-07-19 17:56:20 | [diff] [blame] | 2630 | __begin_node() = static_cast<__iter_pointer>(__np->__parent_); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 2631 | } | 
|  | 2632 | --size(); | 
|  | 2633 | __tree_remove(__end_node()->__left_, | 
|  | 2634 | static_cast<__node_base_pointer>(__np)); | 
| Marshall Clow | 01c1c6f | 2015-01-28 19:54:25 | [diff] [blame] | 2635 | return __node_holder(__np, _Dp(__node_alloc(), true)); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 2636 | } | 
|  | 2637 |  | 
| Howard Hinnant | 7686add | 2011-06-04 14:31:57 | [diff] [blame] | 2638 | template <class _Tp, class _Compare, class _Allocator> | 
|  | 2639 | inline _LIBCPP_INLINE_VISIBILITY | 
|  | 2640 | void | 
|  | 2641 | swap(__tree<_Tp, _Compare, _Allocator>& __x, | 
|  | 2642 | __tree<_Tp, _Compare, _Allocator>& __y) | 
|  | 2643 | _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y))) | 
|  | 2644 | { | 
|  | 2645 | __x.swap(__y); | 
|  | 2646 | } | 
|  | 2647 |  | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 2648 | _LIBCPP_END_NAMESPACE_STD | 
|  | 2649 |  | 
|  | 2650 | #endif // _LIBCPP___TREE |